home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnuchess.lha / gnuchess.c < prev    next >
C/C++ Source or Header  |  1990-06-09  |  78KB  |  3,394 lines

  1. /*
  2.   gnuchess.c - C source for GNU CHESS
  3.  
  4.   Revision: 1990-04-18
  5.  
  6.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  7.   Copyright (c) 1988, 1989, 1990  John Stanback
  8.  
  9.   This file is part of CHESS.
  10.  
  11.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  12.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  13.   the consequences of using it or for whether it serves any particular
  14.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  15.   General Public License for full details.
  16.  
  17.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  18.   only under the conditions described in the CHESS General Public License.
  19.   A copy of this license is supposed to have been given to you along with
  20.   CHESS so you can know your rights and responsibilities.  It should be in a
  21.   file named COPYING.  Among other things, the copyright notice and this
  22.   notice must be preserved on all copies.
  23. */
  24.  
  25.  
  26. #include "gnuchess.h"
  27.  
  28. #include <ctype.h>
  29.  
  30. #ifdef MSDOS
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #define RWA_ACC "r+b"
  35. #define WA_ACC "w+b"
  36. #else
  37. #define RWA_ACC "r+"
  38. #define WA_ACC "w+"
  39. #include <sys/param.h>
  40. #include <sys/types.h>
  41. #include <sys/times.h>
  42. #endif /* MSDOS */
  43.  
  44. /* <stdlib.h> */
  45. extern int abs (int);
  46. extern int atoi (const char *);
  47. /* <time.h> */
  48. extern long int time (long int *);
  49. /* <string.h> */
  50. extern void *memset (void *, int, size_t);
  51.  
  52. #define valueP 100
  53. #define valueN 350
  54. #define valueB 355
  55. #define valueR 550
  56. #define valueQ 1100
  57. #define valueK 1200
  58. #define ctlP 0x4000
  59. #define ctlN 0x2800
  60. #define ctlB 0x1800
  61. #define ctlR 0x0400
  62. #define ctlQ 0x0200
  63. #define ctlK 0x0100
  64. #define ctlBQ 0x1200
  65. #define ctlBN 0x0800
  66. #define ctlRQ 0x0600
  67. #define ctlNN 0x2000
  68. #define Patak(c, u) (atak[c][u] > ctlP)
  69. #define Anyatak(c, u) (atak[c][u] > 0)
  70.  
  71. #if ttblsz
  72. #define truescore 0x0001
  73. #define lowerbound 0x0002
  74. #define upperbound 0x0004
  75. #define kingcastle 0x0008
  76. #define queencastle 0x0010
  77.  
  78. struct hashval
  79. {
  80.   unsigned long key,bd;
  81. };
  82. struct hashentry
  83. {
  84.   unsigned long hashbd;
  85.   unsigned short mv;
  86.   unsigned char flags, depth;    /* char saves some space */
  87.   short score;
  88. #ifdef HASHTEST
  89.   unsigned char bd[32];
  90. #endif /* HASHTEST */
  91.  
  92. };
  93.  
  94. #ifdef HASHFILE
  95. /*
  96.   persistent transposition table.
  97.   The size must be a power of 2. If you change the size,
  98.   be sure to run gnuchess -t before anything else.
  99. */
  100. #define frehash 6
  101. #ifdef MSDOS
  102. #define filesz (1 << 11)
  103. #else
  104. #define filesz (1 << 17)
  105. #endif /* MSDOS */
  106. struct fileentry
  107. {
  108.   unsigned char bd[32];
  109.   unsigned char f, t, flags, depth, sh, sl;
  110. };
  111. /*
  112.   In a networked enviroment gnuchess might be compiled on different
  113.   hosts with different random number generators, that is not acceptable
  114.   if they are going to share the same transposition table.
  115. */
  116. unsigned long int next = 1;
  117.  
  118. unsigned int urand (void)
  119. {
  120.   next *= 1103515245;
  121.   next += 12345;
  122.   return ((unsigned int) (next >> 16) & 0xFFFF);
  123. }
  124.  
  125. void srand (unsigned int seed)
  126. {
  127.   next = seed;
  128. }
  129. #else
  130. #define urand rand
  131. #endif /* HASHFILE */
  132.  
  133. static unsigned long hashkey, hashbd;
  134. static struct hashval hashcode[2][7][64];
  135. static struct hashentry huge ttable[2][ttblsz];
  136. #endif /* ttblsz */
  137. static short rpthash[2][256];
  138.  
  139. FILE *hashfile;
  140. struct leaf Tree[2000], *root;
  141. short TrPnt[maxdepth];
  142. short PieceList[2][16], PawnCnt[2][8];
  143. #define wking PieceList[white][0]
  144. #define bking PieceList[black][0]
  145. #define EnemyKing PieceList[c2][0]
  146. short castld[2], Mvboard[64];
  147. short svalue[64];
  148. struct flags flag;
  149. short opponent, computer, Awindow, Bwindow, dither, INCscore;
  150. long ResponseTime, ExtraTime, Level, et, et0, time0, ft;
  151. long NodeCnt, ETnodes, EvalNodes, HashCnt, FHashCnt, HashCol;
  152. short player, xwndw, rehash;
  153. struct GameRec GameList[512];
  154. short Sdepth, GameCnt, Game50, MaxSearchDepth;
  155. short epsquare, contempt;
  156. struct BookEntry *Book;
  157. struct TimeControlRec TimeControl;
  158. short TCflag, TCmoves, TCminutes, OperatorTime;
  159. const short otherside[3] =
  160. {1, 0, 2};
  161. unsigned short hint, PrVar[maxdepth];
  162.  
  163. static short Pindex[64];
  164. static short PieceCnt[2];
  165. static short c1, c2, *atk1, *atk2, *PC1, *PC2, atak[2][64];
  166. static short mtl[2], pmtl[2], emtl[2], hung[2];
  167. static short FROMsquare, TOsquare, Zscore, zwndw;
  168. static short HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  169. static short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  170. static short Pscore[maxdepth], Tscore[maxdepth];
  171. static const short qrook[3] =
  172. {0, 56, 0};
  173. static const short krook[3] =
  174. {7, 63, 0};
  175. static const short kingP[3] =
  176. {4, 60, 0};
  177. static const short rank7[3] =
  178. {6, 1, 0};
  179. static const short sweep[8] =
  180. {false, false, false, true, true, true, false, false};
  181. static unsigned short killr0[maxdepth], killr1[maxdepth];
  182. static unsigned short killr2[maxdepth], killr3[maxdepth];
  183. static unsigned short PV, Swag0, Swag1, Swag2, Swag3, Swag4;
  184. static unsigned char history[8192];
  185.  
  186. static short Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  187. static short Mking[2][64], Kfield[2][64];
  188. static const short value[7] =
  189. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  190. static const short control[7] =
  191. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  192. static const short PassedPawn0[8] =
  193. {0, 60, 80, 120, 200, 360, 600, 800};
  194. static const short PassedPawn1[8] =
  195. {0, 30, 40, 60, 100, 180, 300, 800};
  196. static const short PassedPawn2[8] =
  197. {0, 15, 25, 35, 50, 90, 140, 800};
  198. static const short PassedPawn3[8] =
  199. {0, 5, 10, 15, 20, 30, 140, 800};
  200. static const short ISOLANI[8] =
  201. {-12, -16, -20, -24, -24, -20, -16, -12};
  202. static const short BACKWARD[16] =
  203. {-6, -10, -15, -21, -28, -28, -28, -28,
  204.  -28, -28, -28, -28, -28, -28, -28, -28};
  205. static const short BMBLTY[14] =
  206. {-2, 0, 2, 4, 6, 8, 10, 12, 13, 14, 15, 16, 16, 16};
  207. static const short RMBLTY[15] =
  208. {0, 2, 4, 6, 8, 10, 11, 12, 13, 14, 14, 14, 14, 14, 14};
  209. static const short KTHRT[36] =
  210. {0, -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
  211.  -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
  212.  -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80};
  213. static short KNIGHTPOST, KNIGHTSTRONG, BISHOPSTRONG, KATAK;
  214. static short PEDRNK2B, PWEAKH, PADVNCM, PADVNCI, PAWNSHIELD, PDOUBLED, PBLOK;
  215. static short RHOPN, RHOPNX, KHOPN, KHOPNX, KSFTY;
  216. static short ATAKD, HUNGP, HUNGX, KCASTLD, KMOVD, XRAY, PINVAL;
  217. static short stage, stage2, Developed[2];
  218. static short PawnBonus, BishopBonus, RookBonus;
  219. static const short KingOpening[64] =
  220. {0, 0, -4, -10, -10, -4, 0, 0,
  221.  -4, -4, -8, -12, -12, -8, -4, -4,
  222.  -12, -16, -20, -20, -20, -20, -16, -12,
  223.  -16, -20, -24, -24, -24, -24, -20, -16,
  224.  -16, -20, -24, -24, -24, -24, -20, -16,
  225.  -12, -16, -20, -20, -20, -20, -16, -12,
  226.  -4, -4, -8, -12, -12, -8, -4, -4,
  227.  0, 0, -4, -10, -10, -4, 0, 0};
  228. static const short KingEnding[64] =
  229. {0, 6, 12, 18, 18, 12, 6, 0,
  230.  6, 12, 18, 24, 24, 18, 12, 6,
  231.  12, 18, 24, 30, 30, 24, 18, 12,
  232.  18, 24, 30, 36, 36, 30, 24, 18,
  233.  18, 24, 30, 36, 36, 30, 24, 18,
  234.  12, 18, 24, 30, 30, 24, 18, 12,
  235.  6, 12, 18, 24, 24, 18, 12, 6,
  236.  0, 6, 12, 18, 18, 12, 6, 0};
  237. static const short DyingKing[64] =
  238. {0, 8, 16, 24, 24, 16, 8, 0,
  239.  8, 32, 40, 48, 48, 40, 32, 8,
  240.  16, 40, 56, 64, 64, 56, 40, 16,
  241.  24, 48, 64, 72, 72, 64, 48, 24,
  242.  24, 48, 64, 72, 72, 64, 48, 24,
  243.  16, 40, 56, 64, 64, 56, 40, 16,
  244.  8, 32, 40, 48, 48, 40, 32, 8,
  245.  0, 8, 16, 24, 24, 16, 8, 0};
  246. static const short KBNK[64] =
  247. {99, 90, 80, 70, 60, 50, 40, 40,
  248.  90, 80, 60, 50, 40, 30, 20, 40,
  249.  80, 60, 40, 30, 20, 10, 30, 50,
  250.  70, 50, 30, 10, 0, 20, 40, 60,
  251.  60, 40, 20, 0, 10, 30, 50, 70,
  252.  50, 30, 10, 20, 30, 40, 60, 80,
  253.  40, 20, 30, 40, 50, 60, 80, 90,
  254.  40, 40, 50, 60, 70, 80, 90, 99};
  255. static const short pknight[64] =
  256. {0, 4, 8, 10, 10, 8, 4, 0,
  257.  4, 8, 16, 20, 20, 16, 8, 4,
  258.  8, 16, 24, 28, 28, 24, 16, 8,
  259.  10, 20, 28, 32, 32, 28, 20, 10,
  260.  10, 20, 28, 32, 32, 28, 20, 10,
  261.  8, 16, 24, 28, 28, 24, 16, 8,
  262.  4, 8, 16, 20, 20, 16, 8, 4,
  263.  0, 4, 8, 10, 10, 8, 4, 0};
  264. static const short pbishop[64] =
  265. {14, 14, 14, 14, 14, 14, 14, 14,
  266.  14, 22, 18, 18, 18, 18, 22, 14,
  267.  14, 18, 22, 22, 22, 22, 18, 14,
  268.  14, 18, 22, 22, 22, 22, 18, 14,
  269.  14, 18, 22, 22, 22, 22, 18, 14,
  270.  14, 18, 22, 22, 22, 22, 18, 14,
  271.  14, 22, 18, 18, 18, 18, 22, 14,
  272.  14, 14, 14, 14, 14, 14, 14, 14};
  273. static const short PawnAdvance[64] =
  274. {0, 0, 0, 0, 0, 0, 0, 0,
  275.  4, 4, 4, 0, 0, 4, 4, 4,
  276.  6, 8, 2, 10, 10, 2, 8, 6,
  277.  6, 8, 12, 16, 16, 12, 8, 6,
  278.  8, 12, 16, 24, 24, 16, 12, 8,
  279.  12, 16, 24, 32, 32, 24, 16, 12,
  280.  12, 16, 24, 32, 32, 24, 16, 12,
  281.  0, 0, 0, 0, 0, 0, 0, 0};
  282.  
  283.  
  284. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  285.  
  286.  
  287. #define taxicab(a,b) taxidata[a][b]
  288. short distdata[64][64], taxidata[64][64];
  289.  
  290. static inline void
  291. Initialize_dist (void)
  292. {
  293.   register short a, b, d, di;
  294.  
  295.   for (a = 0; a < 64; a++)
  296.     for (b = 0; b < 64; b++)
  297.       {
  298.     d = abs (column (a) - column (b));
  299.     di = abs (row (a) - row (b));
  300.     taxidata[a][b] = d + di;
  301.     distdata[a][b] = (d > di ? d : di);
  302.       }
  303. }
  304.  
  305. const short Stboard[64] =
  306. {rook, knight, bishop, queen, king, bishop, knight, rook,
  307.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  308.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  309.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  310.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  311.  rook, knight, bishop, queen, king, bishop, knight, rook};
  312. const short Stcolor[64] =
  313. {white, white, white, white, white, white, white, white,
  314.  white, white, white, white, white, white, white, white,
  315.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  316.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  317.  black, black, black, black, black, black, black, black,
  318.  black, black, black, black, black, black, black, black};
  319. short board[64], color[64];
  320. static unsigned char nextpos[8][64][64];
  321. static unsigned char nextdir[8][64][64];
  322. /*
  323.   ptype is used to separate white and black pawns, like this;
  324.   ptyp = ptype[side][piece]
  325.   piece can be used directly in nextpos/nextdir when generating moves
  326.   for pieces that are not black pawns.
  327. */
  328. static const short ptype[2][8] =
  329. {
  330.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  331.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  332. static const short direc[8][8] =
  333. {
  334.   0, 0, 0, 0, 0, 0, 0, 0,
  335.   10, 9, 11, 0, 0, 0, 0, 0,
  336.   8, -8, 12, -12, 19, -19, 21, -21,
  337.   9, 11, -9, -11, 0, 0, 0, 0,
  338.   1, 10, -1, -10, 0, 0, 0, 0,
  339.   1, 10, -1, -10, 9, 11, -9, -11,
  340.   1, 10, -1, -10, 9, 11, -9, -11,
  341.   -10, -9, -11, 0, 0, 0, 0, 0};
  342. static const short max_steps[8] =
  343. {0, 2, 1, 7, 7, 7, 1, 2};
  344. static const short nunmap[120] =
  345. {
  346.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  347.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  348.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  349.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  350.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  351.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  352.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  353.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  354.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  355.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  356.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  357.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  358.  
  359.  
  360. void
  361. Initialize_moves (void)
  362.  
  363. /*
  364.   This procedure pre-calculates all moves for every piece from every square.
  365.   This data is stored in nextpos/nextdir and used later in the move generation
  366.   routines.
  367. */
  368.  
  369. {
  370.   short ptyp, po, p0, d, di, s, delta;
  371.   unsigned char *ppos, *pdir;
  372.   short dest[8][8];
  373.   short steps[8];
  374.   short sorted[8];
  375.  
  376.   for (ptyp = 0; ptyp < 8; ptyp++)
  377.     for (po = 0; po < 64; po++)
  378.       for (p0 = 0; p0 < 64; p0++)
  379.     {
  380.       nextpos[ptyp][po][p0] = (unsigned char) po;
  381.       nextdir[ptyp][po][p0] = (unsigned char) po;
  382.     }
  383.   for (ptyp = 1; ptyp < 8; ptyp++)
  384.     for (po = 21; po < 99; po++)
  385.       if (nunmap[po] >= 0)
  386.     {
  387.       ppos = nextpos[ptyp][nunmap[po]];
  388.       pdir = nextdir[ptyp][nunmap[po]];
  389.       /* dest is a function of direction and steps */
  390.       for (d = 0; d < 8; d++)
  391.         {
  392.           dest[d][0] = nunmap[po];
  393.           delta = direc[ptyp][d];
  394.           if (delta != 0)
  395.         {
  396.           p0 = po;
  397.           for (s = 0; s < max_steps[ptyp]; s++)
  398.             {
  399.               p0 = p0 + delta;
  400.               /*
  401.             break if (off board) or
  402.             (pawns only move two steps from home square)
  403.               */
  404.               if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  405.               && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  406.             break;
  407.               else
  408.             dest[d][s] = nunmap[p0];
  409.             }
  410.         }
  411.           else
  412.         s = 0;
  413.  
  414.           /*
  415.             sort dest in number of steps order
  416.             currently no sort is done due to compability with
  417.             the move generation order in old gnu chess
  418.           */
  419.           steps[d] = s;
  420.           for (di = d; s > 0 && di > 0; di--)
  421.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  422.           sorted[di] = sorted[di - 1];
  423.         else
  424.           break;
  425.           sorted[di] = d;
  426.         }
  427.  
  428.       /*
  429.         update nextpos/nextdir,
  430.         pawns have two threads (capture and no capture)
  431.       */
  432.       p0 = nunmap[po];
  433.       if (ptyp == pawn || ptyp == bpawn)
  434.         {
  435.           for (s = 0; s < steps[0]; s++)
  436.         {
  437.           ppos[p0] = (unsigned char) dest[0][s];
  438.           p0 = dest[0][s];
  439.         }
  440.           p0 = nunmap[po];
  441.           for (d = 1; d < 3; d++)
  442.         {
  443.           pdir[p0] = (unsigned char) dest[d][0];
  444.           p0 = dest[d][0];
  445.         }
  446.         }
  447.       else
  448.         {
  449.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  450.           for (d = 0; d < 8; d++)
  451.         for (s = 0; s < steps[sorted[d]]; s++)
  452.           {
  453.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  454.             p0 = dest[sorted[d]][s];
  455.             if (d < 7)
  456.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  457.             /* else is already initialized */
  458.           }
  459.         }
  460.     }
  461. }
  462.  
  463. /* hmm.... shouldn`t main be moved to the interface routines */
  464. int
  465. main (int argc, char **argv)
  466. {
  467.   short int ahead = true, hash = true;
  468.   char *xwin = 0;
  469.  
  470.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  471.     {
  472.       switch (argv[1][1])
  473.     {
  474.     case 'a':
  475.       ahead = (argv[1][0] == '-') ? false : true;
  476.       break;
  477.     case 'h':
  478.       hash = (argv[1][0] == '-') ? false : true;
  479.       break;
  480. #if ttblsz
  481. #ifdef HASHFILE
  482.     case 't': /* create or test persistent transposition table */ 
  483.       {
  484.         if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  485.           hashfile = fopen (HASHFILE , WA_ACC);
  486.         if (hashfile != NULL)
  487.           {
  488.         long i, j;
  489.         int nr[maxdepth];
  490.         struct fileentry n;
  491.         
  492.         printf("Counting transposition file entries, wait!\n");
  493.         for(i = 0; i < maxdepth; i++) nr[i] = 0;
  494.         fseek(hashfile, 0L, SEEK_END);
  495.         i = ftell(hashfile) / sizeof(struct fileentry);
  496.         fseek(hashfile, 0L, SEEK_SET);
  497.         for (j = 0; j < i; j++)
  498.           {
  499.             fread(&n, sizeof(struct fileentry), 1, hashfile);
  500.             if (n.depth)
  501.               {
  502.             nr[n.depth]++;
  503.             nr[0]++;
  504.               }
  505.           }
  506.         printf("The file contains %d entries out of max %d\n",
  507.                nr[0], i);
  508.         for(j = 1; j < maxdepth; j++) printf("%d ",nr[j]);
  509.         printf("\n");
  510.         if (i < filesz)
  511.           {
  512.             printf("Filling transposition file, wait!\n");
  513.             for(j = 0; j < 32; j++)
  514.               n.bd[j] = 0;
  515.             n.f = n.t = 0;
  516.             n.flags = 0;
  517.             n.depth = 0;
  518.             n.sh = n.sl = 0;
  519.             for (j = i; j < filesz; j++)
  520.               fwrite (&n, sizeof(struct fileentry), 1, hashfile);
  521.           }
  522.         fclose(hashfile);
  523.           }
  524.         return(0);
  525.         break;
  526.       }
  527. #endif /* HASHFILE */
  528. #endif /* ttblsz */
  529.     case 'x':
  530.       xwin = &argv[1][2];
  531.       break;
  532.     default:
  533.       fprintf (stderr, "Usage: gnuchess [-a] [-t] [-x xwndw]\n");
  534.     }
  535.       argv++;
  536.       argc--;
  537.     }
  538.   Level = 0;
  539.   TCflag = false;
  540.   OperatorTime = 0;
  541.   if (argc == 2)
  542.     Level = atoi (argv[1]);
  543.   if (argc == 3)
  544.     {
  545.       TCmoves = atoi (argv[1]);
  546.       TCminutes = atoi (argv[2]);
  547.       TCflag = true;
  548.     }
  549.   Initialize ();
  550.   Initialize_dist ();
  551.   Initialize_moves ();
  552.   NewGame ();
  553.   GetOpenings ();
  554.  
  555.   flag.easy = ahead;
  556.   flag.hash = hash;
  557.   if (xwin)
  558.     xwndw = atoi (xwin);
  559.  
  560.   hashfile = NULL;
  561. #if ttblsz
  562. #ifdef HASHFILE
  563.   hashfile = fopen (HASHFILE, RWA_ACC);
  564. #endif /* HASHFILE */
  565. #endif  /* ttblsz */
  566.   while (!(flag.quit))
  567.     {
  568.       if (flag.bothsides && !flag.mate)
  569.     SelectMove (opponent, 1);
  570.       else
  571.     InputCommand ();
  572.       if (!(flag.quit || flag.mate || flag.force))
  573.     SelectMove (computer, 1);
  574.     }
  575. #if ttblsz
  576. #ifdef HASHFILE
  577.   if (hashfile) fclose(hashfile);
  578. #endif /* HASHFILE */
  579. #endif /* ttblsz */
  580.  
  581.   ExitChess ();
  582.   return (0);
  583. }
  584.  
  585. void
  586. NewGame (void)
  587.  
  588. /*
  589.   Reset the board and other variables to start a new game.
  590. */
  591.  
  592. {
  593.   short l, c, p;
  594.  
  595.   stage = stage2 = -1;        /* the game is not yet started */
  596.   flag.mate = flag.post = flag.quit = flag.reverse = flag.bothsides = false;
  597.   flag.force = false;
  598.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  599.   NodeCnt = et0 = epsquare = 0;
  600.   dither = 0;
  601.   Awindow = 90;
  602.   Bwindow = 90;
  603.   xwndw = 90;
  604.   MaxSearchDepth = 29;
  605.   contempt = 0;
  606.   GameCnt = 0;
  607.   Game50 = 1;
  608.   hint = 0x0C14;
  609.   ZeroRPT ();
  610.   Developed[white] = Developed[black] = false;
  611.   castld[white] = castld[black] = false;
  612.   PawnThreat[0] = CptrFlag[0] = false;
  613.   Pscore[0] = 12000;
  614.   Tscore[0] = 12000;
  615.   opponent = white;
  616.   computer = black;
  617.   for (l = 0; l < 2000; l++)
  618.     Tree[l].f = Tree[l].t = 0;
  619. #if ttblsz
  620.   rehash = 6;
  621.   ZeroTTable ();
  622.   srand ((unsigned int) 1);
  623.   for (c = white; c <= black; c++)
  624.     for (p = pawn; p <= king; p++)
  625.       for (l = 0; l < 64; l++)
  626.     {
  627.       hashcode[c][p][l].key = (((unsigned long) urand ()));
  628.       hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  629.       hashcode[c][p][l].bd = (((unsigned long) urand ()));
  630.       hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  631.       if (sizeof(long) > 4)
  632.         {
  633.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 32);
  634.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 48);
  635.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 32);
  636.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 48);
  637.         }
  638.     }
  639. #endif /* ttblsz */
  640.   for (l = 0; l < 64; l++)
  641.     {
  642.       board[l] = Stboard[l];
  643.       color[l] = Stcolor[l];
  644.       Mvboard[l] = 0;
  645.     }
  646.   ClrScreen ();
  647.   if (TCflag)
  648.     SetTimeControl ();
  649.   else if (Level == 0)
  650.     SelectLevel ();
  651.   InitializeStats ();
  652.   time0 = time ((long *) 0);
  653.   ElapsedTime (1);
  654.   UpdateDisplay (0, 0, 1, 0);
  655. }
  656.  
  657.  
  658. /* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */
  659.  
  660. static inline void
  661. pick (short int p1, short int p2)
  662.  
  663. /*
  664.   Find the best move in the tree between indexes p1 and p2. Swap the best
  665.   move into the p1 element.
  666. */
  667.  
  668. {
  669.   register short p, s;
  670.   short p0, s0;
  671.   struct leaf temp;
  672.  
  673.   s0 = Tree[p1].score;
  674.   p0 = p1;
  675.   for (p = p1 + 1; p <= p2; p++)
  676.     if ((s = Tree[p].score) > s0)
  677.       {
  678.     s0 = s;
  679.     p0 = p;
  680.       }
  681.   if (p0 != p1)
  682.     {
  683.       temp = Tree[p1];
  684.       Tree[p1] = Tree[p0];
  685.       Tree[p0] = temp;
  686.     }
  687. }
  688.  
  689. void
  690. SelectMove (short int side, short int iop)
  691.  
  692.  
  693. /*
  694.   Select a move by calling function search() at progressively deeper ply
  695.   until time is up or a mate or draw is reached. An alpha-beta window of -90
  696.   to +90 points is set around the score returned from the previous
  697.   iteration. If Sdepth != 0 then the program has correctly predicted the
  698.   opponents move and the search will start at a depth of Sdepth+1 rather
  699.   than a depth of 1.
  700. */
  701.  
  702. {
  703.   static short i, tempb, tempc, tempsf, tempst, xside, rpt;
  704.   static short alpha, beta, score;
  705.  
  706.   flag.timeout = false;
  707.   xside = otherside[side];
  708.   if (iop != 2)
  709.     player = side;
  710.   if (TCflag)
  711.     {
  712.       if ((TimeControl.moves[side] + 3) != 0)
  713.     ResponseTime = (TimeControl.clock[side]) /
  714.       (TimeControl.moves[side] + 3) -
  715.       OperatorTime;
  716.       else
  717.     ResponseTime = 0;
  718.       ResponseTime += (ResponseTime * TimeControl.moves[side]) / (2 * TCmoves + 1);
  719.     }
  720.   else
  721.     ResponseTime = Level;
  722.   if (iop == 2)
  723.     ResponseTime = 99999;
  724.   if (Sdepth > 0 && root->score > Zscore - zwndw)
  725.     ResponseTime -= ft;
  726.   else if (ResponseTime < 1)
  727.     ResponseTime = 1;
  728.   ExtraTime = 0;
  729.   ExaminePosition ();
  730.   ScorePosition (side, &score);
  731.   /* Pscore[0] = -score; */
  732.   ShowSidetomove ();
  733.  
  734.   if (Sdepth == 0)
  735.     {
  736. #if ttblsz
  737.       /* ZeroTTable (); */
  738. #endif /* ttblsz */
  739.       SearchStartStuff (side);
  740. #ifdef NOMEMSET
  741.       for (i = 0; i < 8192; i++)
  742.     history[i] = 0;
  743. #else
  744.       memset ((char *) history, 0, sizeof (history));
  745. #endif /* NOMEMSET */
  746.       FROMsquare = TOsquare = -1;
  747.       PV = 0;
  748.       if (iop != 2)
  749.     hint = 0;
  750.       for (i = 0; i < maxdepth; i++)
  751.     PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;
  752.       alpha = score - 90;
  753.       beta = score + 90;
  754.       rpt = 0;
  755.       TrPnt[1] = 0;
  756.       root = &Tree[0];
  757.       MoveList (side, 1);
  758.       for (i = TrPnt[1]; i < TrPnt[2]; i++)
  759.     pick (i, TrPnt[2] - 1);
  760.       if (Book != NULL)
  761.     OpeningBook (&hint);
  762.       if (Book != NULL)
  763.     flag.timeout = true;
  764.       NodeCnt = ETnodes = EvalNodes = HashCnt = FHashCnt = HashCol = 0;
  765.       Zscore = 0;
  766.       zwndw = 20;
  767.     }
  768.   while (!flag.timeout && Sdepth < MaxSearchDepth)
  769.     {
  770.       Sdepth++;
  771.       ShowDepth (' ');
  772.       score = search (side, 1, Sdepth, alpha, beta, PrVar, &rpt);
  773.       for (i = 1; i <= Sdepth; i++)
  774.     killr0[i] = PrVar[i];
  775.       if (score < alpha)
  776.     {
  777.       ShowDepth ('-');
  778.       ExtraTime = 10 * ResponseTime;
  779.       /* ZeroTTable (); */
  780.       score = search (side, 1, Sdepth, -9000, score, PrVar, &rpt);
  781.     }
  782.       if (score > beta && !(root->flags & exact))
  783.     {
  784.       ShowDepth ('+');
  785.       ExtraTime = 0;
  786.       /* ZeroTTable (); */
  787.       score = search (side, 1, Sdepth, score, 9000, PrVar, &rpt);
  788.     }
  789.       score = root->score;
  790.       if (!flag.timeout)
  791.     for (i = TrPnt[1] + 1; i < TrPnt[2]; i++)
  792.       pick (i, TrPnt[2] - 1);
  793.       ShowResults (score, PrVar, '.');
  794.       for (i = 1; i <= Sdepth; i++)
  795.     killr0[i] = PrVar[i];
  796.       if (score > Zscore - zwndw && score > Tree[1].score + 250)
  797.     ExtraTime = 0;
  798.       else if (score > Zscore - 3 * zwndw)
  799.     ExtraTime = ResponseTime;
  800.       else
  801.     ExtraTime = 3 * ResponseTime;
  802.       if (root->flags & exact)
  803.     flag.timeout = true;
  804.       if (Tree[1].score < -9000)
  805.     flag.timeout = true;
  806.       if (4 * et > 2 * ResponseTime + ExtraTime)
  807.     flag.timeout = true;
  808.       if (!flag.timeout)
  809.     {
  810.       Tscore[0] = score;
  811.       if (Zscore == 0)
  812.         Zscore = score;
  813.       else
  814.         Zscore = (Zscore + score) / 2;
  815.     }
  816.       zwndw = 20 + abs (Zscore / 12);
  817.       beta = score + Bwindow;
  818.       if (Zscore < score)
  819.     alpha = Zscore - Awindow - zwndw;
  820.       else
  821.     alpha = score - Awindow - zwndw;
  822.     }
  823.  
  824.   score = root->score;
  825.   if (rpt >= 2 || score < -12000)
  826.     root->flags |= draw;
  827.   if (iop == 2)
  828.     return;
  829.   if (Book == NULL)
  830.     hint = PrVar[2];
  831.   ElapsedTime (1);
  832.  
  833.   if (score > -9999 && rpt <= 2)
  834.     {
  835.       MakeMove (side, root, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  836.       algbr (root->f, root->t, (short) root->flags);
  837.     }
  838.   else
  839.     algbr (0, 0, 0);
  840.   OutputMove ();
  841.   if (score == -9999 || score == 9998)
  842.     flag.mate = true;
  843.   if (flag.mate)
  844.     hint = 0;
  845.   if ((board[root->t] == pawn)
  846.       || (root->flags & capture)
  847.       || (root->flags & cstlmask))
  848.     {
  849.       Game50 = GameCnt;
  850.       ZeroRPT ();
  851.     }
  852.   GameList[GameCnt].score = score;
  853.   GameList[GameCnt].nodes = NodeCnt;
  854.   GameList[GameCnt].time = (short) et;
  855.   GameList[GameCnt].depth = Sdepth;
  856.   if (TCflag)
  857.     {
  858.       TimeControl.clock[side] -= (et + OperatorTime);
  859.       if (--TimeControl.moves[side] == 0)
  860.     SetTimeControl ();
  861.     }
  862.   if ((root->flags & draw) && flag.bothsides)
  863.     flag.mate = true;
  864.   if (GameCnt > 470)
  865.     flag.mate = true; /* out of move store, you loose */
  866.   player = xside;
  867.   Sdepth = 0;
  868.   fflush (stdin);
  869. }
  870.  
  871. int
  872. parse (FILE *fd, short unsigned int *mv, short int side)
  873. {
  874.   int c, i, r1, r2, c1, c2;
  875.   char s[100];
  876.   while ((c = getc (fd)) == ' ') ;
  877.   i = 0;
  878.   s[0] = (char) c;
  879.   while (c != ' ' && c != '\n' && c != EOF)
  880.     s[++i] = (char) (c = getc (fd));
  881.   s[++i] = '\0';
  882.   if (c == EOF)
  883.     return (-1);
  884.   if (s[0] == '!' || s[0] == ';' || i < 3)
  885.     {
  886.       while (c != '\n' && c != EOF)
  887.     c = getc (fd);
  888.       return (0);
  889.     }
  890.   if (s[4] == 'o')
  891.     if (side == black)
  892.       *mv = 0x3C3A;
  893.     else
  894.       *mv = 0x0402;
  895.   else if (s[0] == 'o')
  896.     if (side == black)
  897.       *mv = 0x3C3E;
  898.     else
  899.       *mv = 0x0406;
  900.   else
  901.     {
  902.       c1 = s[0] - 'a';
  903.       r1 = s[1] - '1';
  904.       c2 = s[2] - 'a';
  905.       r2 = s[3] - '1';
  906.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  907.     }
  908.   return (1);
  909. }
  910.  
  911. void
  912. GetOpenings (void)
  913.      
  914. /*
  915.    Read in the Opening Book file and parse the algebraic notation for a move
  916.    into an unsigned integer format indicating the from and to square. Create
  917.    a linked list of opening lines of play, with entry->next pointing to the
  918.    next line and entry->move pointing to a chunk of memory containing the
  919.    moves. More Opening lines of up to 256 half moves may be added to
  920.    gnuchess.book.
  921. */
  922. #ifndef BOOK
  923. #define BOOK "/usr/games/lib/gnuchess.book"
  924. #endif /* BOOK */     
  925. {
  926.   FILE *fd;
  927.   int c, i, j, side;
  928.   /* char buffr[2048]; */
  929.   struct BookEntry *entry;
  930.   unsigned short mv, *mp, tmp[100];
  931.  
  932.   if ((fd = fopen (BOOK, "r")) == NULL)
  933.     fd = fopen ("gnuchess.book", "r");
  934.   if (fd != NULL)
  935.     {
  936.       /* setvbuf(fd,buffr,_IOFBF,2048); */
  937.       Book = NULL;
  938.       i = 0;
  939.       side = white;
  940.       while ((c = parse (fd, &mv, side)) >= 0)
  941.     if (c == 1)
  942.       {
  943.         tmp[++i] = mv;
  944.         side = otherside[side];
  945.       }
  946.     else if (c == 0 && i > 0)
  947.       {
  948.         entry = (struct BookEntry *) malloc (sizeof (struct BookEntry));
  949.         mp = (unsigned short *) malloc ((i + 1) * sizeof (unsigned short));
  950.         if (!entry || !mp)
  951.           {
  952.         Book = NULL;
  953.         ShowMessage ("warning: can't load book, out of memory.");
  954.         return;
  955.           }
  956.         entry->mv = mp;
  957.         entry->next = Book;
  958.         Book = entry;
  959.         for (j = 1; j <= i; j++)
  960.           *(mp++) = tmp[j];
  961.         *mp = 0;
  962.         i = 0;
  963.         side = white;
  964.       }
  965.       fclose (fd);
  966.     }
  967.   else
  968.     ShowMessage ("warning: can't find book.");
  969. }
  970.  
  971.  
  972. void
  973. OpeningBook (unsigned short *hint)
  974.  
  975. /*
  976.   Go thru each of the opening lines of play and check for a match with the
  977.   current game listing. If a match occurs, generate a random number. If this
  978.   number is the largest generated so far then the next move in this line
  979.   becomes the current "candidate". After all lines are checked, the
  980.   candidate move is put at the top of the Tree[] array and will be played by
  981.   the program. Note that the program does not handle book transpositions.
  982. */
  983.  
  984. {
  985.   short j, pnt;
  986.   unsigned short m, *mp;
  987.   unsigned r, r0;
  988.   struct BookEntry *p;
  989.  
  990.   srand ((unsigned int) time ((long *) 0));
  991.   r0 = m = 0;
  992.   p = Book;
  993.   while (p != NULL)
  994.     {
  995.       mp = p->mv;
  996.       for (j = 1; j <= GameCnt; j++)
  997.     if (GameList[j].gmove != *(mp++))
  998.       break;
  999.       if (j > GameCnt)
  1000.     if ((r = urand ()) > r0)
  1001.       {
  1002.         r0 = r;
  1003.         m = *mp;
  1004.         *hint = *(++mp);
  1005.       }
  1006.       p = p->next;
  1007.     }
  1008.  
  1009.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  1010.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  1011.       Tree[pnt].score = 0;
  1012.   pick (TrPnt[1], TrPnt[2] - 1);
  1013.   if (Tree[TrPnt[1]].score < 0)
  1014.     Book = NULL;
  1015. }
  1016.  
  1017.  
  1018. inline void
  1019. repetition (short int *cnt)
  1020.  
  1021. /*
  1022.   Check for draw by threefold repetition.
  1023. */
  1024.  
  1025. {
  1026.   register short i, c, f, t;
  1027.   short b[64];
  1028.   unsigned short m;
  1029.  
  1030.   *cnt = c = 0;
  1031.   if (GameCnt > Game50 + 3)
  1032.     {
  1033. #ifdef NOMEMSET
  1034.       for (i = 0; i < 64; b[i++] = 0) ;
  1035. #else
  1036.       memset ((char *) b, 0, sizeof (b));
  1037. #endif /* NOMEMSET */
  1038.       for (i = GameCnt; i > Game50; i--)
  1039.     {
  1040.       m = GameList[i].gmove;
  1041.       f = m >> 8;
  1042.       t = m & 0xFF;
  1043.       if (++b[f] == 0)
  1044.         c--;
  1045.       else
  1046.         c++;
  1047.       if (--b[t] == 0)
  1048.         c--;
  1049.       else
  1050.         c++;
  1051.       if (c == 0)
  1052.         (*cnt)++;
  1053.     }
  1054.     }
  1055. }
  1056.  
  1057. int
  1058. search (short int side,
  1059.     short int ply,
  1060.     short int depth,
  1061.     short int alpha,
  1062.     short int beta,
  1063.     short unsigned int *bstline,
  1064.     short int *rpt)
  1065.  
  1066. /*
  1067.   Perform an alpha-beta search to determine the score for the current board
  1068.   position. If depth <= 0 only capturing moves, pawn promotions and
  1069.   responses to check are generated and searched, otherwise all moves are
  1070.   processed. The search depth is modified for check evasions, certain
  1071.   re-captures and threats. Extensions may continue for up to 11 ply beyond
  1072.   the nominal search depth.
  1073. */
  1074.  
  1075. #define UpdateSearchStatus \
  1076. {\
  1077.    if (flag.post) ShowCurrentMove(pnt,node->f,node->t);\
  1078.      if (pnt > TrPnt[1])\
  1079.        {\
  1080.       d = best-Zscore; e = best-node->score;\
  1081.         if (best < alpha) ExtraTime = 10*ResponseTime;\
  1082.         else if (d > -zwndw && e > 4*zwndw) ExtraTime = -ResponseTime/3;\
  1083.         else if (d > -zwndw) ExtraTime = 0;\
  1084.         else if (d > -3*zwndw) ExtraTime = ResponseTime;\
  1085.         else if (d > -9*zwndw) ExtraTime = 3*ResponseTime;\
  1086.         else ExtraTime = 5*ResponseTime;\
  1087.         }\
  1088.         }
  1089. #define prune (cf && score+node->score < alpha)
  1090. #define ReCapture (flag.rcptr && score > alpha && score < beta &&\
  1091.            ply > 2 && CptrFlag[ply-1] && CptrFlag[ply-2])
  1092. /* && depth == Sdepth-ply+1 */
  1093. #define Parry (hung[side] > 1 && ply == Sdepth+1)
  1094. #define MateThreat (ply < Sdepth+4 && ply > 4 &&\
  1095.             ChkFlag[ply-2] && ChkFlag[ply-4] &&\
  1096.             ChkFlag[ply-2] != ChkFlag[ply-4])
  1097.  
  1098. {
  1099.   register short j, pnt;
  1100.   short best, tempb, tempc, tempsf, tempst;
  1101.   short xside, pbst, d, e, cf, score, rcnt, slk, InChk;
  1102.   unsigned short mv, nxtline[maxdepth];
  1103.   struct leaf *node, tmp;
  1104.  
  1105.   NodeCnt++;
  1106.   xside = otherside[side];
  1107.  
  1108.   if ((ply <= Sdepth + 3) && rpthash[side][hashkey & 0xFF] > 0)
  1109.     repetition (rpt);
  1110.   else
  1111.     *rpt = 0;
  1112.   /* Detect repetitions a bit earlier. SMC. 12/89 */
  1113.   if (*rpt == 1 && ply > 1)
  1114.     return (0);
  1115.   /* if (*rpt >= 2) return(0); */
  1116.  
  1117.   score = evaluate (side, ply, alpha, beta, INCscore, &slk, &InChk);
  1118.   if (score > 9000)
  1119.     {
  1120.       bstline[ply] = 0;
  1121.       return (score);
  1122.     }
  1123.   if (depth > 0)
  1124.     {
  1125.       /* Allow opponent a chance to check again */
  1126.       if (InChk)
  1127.     depth = (depth < 2) ? 2 : depth;
  1128.       else if (PawnThreat[ply - 1] || ReCapture)
  1129.     ++depth;
  1130.     }
  1131.   else
  1132.     {
  1133.       if (score >= alpha &&
  1134.       (InChk || PawnThreat[ply - 1] || Parry))
  1135.     depth = 1;
  1136.       else if (score <= beta && MateThreat)
  1137.     depth = 1;
  1138.     }
  1139.  
  1140. #if ttblsz
  1141.   if (depth > 0 && flag.hash && ply > 1)
  1142.     {
  1143.       if (ProbeTTable (side, depth, &alpha, &beta, &score) == false)
  1144. #ifdef HASHFILE    
  1145.     if (hashfile && (depth > 5) && (GameCnt < 12))
  1146.       ProbeFTable (side, depth, &alpha, &beta, &score);
  1147. #else
  1148.       /* do nothing */;
  1149. #endif /* HASHFILE */      
  1150.       bstline[ply] = PV;
  1151.       bstline[ply + 1] = 0;
  1152.       if (beta == -20000)
  1153.     return (score);
  1154.       if (alpha > beta)
  1155.     return (alpha);
  1156.     }
  1157. #endif /* ttblsz */
  1158.   if (Sdepth == 1)
  1159.     d = 7;
  1160.   else
  1161.     d = 11;
  1162.   if (ply > Sdepth + d || (depth < 1 && score > beta))
  1163.     /* score >= beta ?? */
  1164.     return (score);
  1165.  
  1166.   if (ply > 1)
  1167.     if (depth > 0)
  1168.       MoveList (side, ply);
  1169.     else
  1170.       CaptureList (side, ply);
  1171.  
  1172.   if (TrPnt[ply] == TrPnt[ply + 1])
  1173.     return (score);
  1174.  
  1175.   cf = (depth < 1 && ply > Sdepth + 1 && !ChkFlag[ply - 2] && !slk);
  1176.  
  1177.   if (depth > 0)
  1178.     best = -12000;
  1179.   else
  1180.     best = score;
  1181.   if (best > alpha)
  1182.     alpha = best;
  1183.  
  1184.   for (pnt = pbst = TrPnt[ply];
  1185.        pnt < TrPnt[ply + 1] && best <= beta;    /* best < beta ?? */
  1186.        pnt++)
  1187.     {
  1188.       if (ply > 1)
  1189.     pick (pnt, TrPnt[ply + 1] - 1);
  1190.       node = &Tree[pnt];
  1191.       mv = (node->f << 8) | node->t;
  1192.       nxtline[ply + 1] = 0;
  1193.  
  1194.       if (prune)
  1195.     break;
  1196.       if (ply == 1)
  1197.     UpdateSearchStatus;
  1198.  
  1199.       if (!(node->flags & exact))
  1200.     {
  1201.       MakeMove (side, node, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  1202.       CptrFlag[ply] = (node->flags & capture);
  1203.       PawnThreat[ply] = (node->flags & pwnthrt);
  1204.       Tscore[ply] = node->score;
  1205.       PV = node->reply;
  1206.       node->score = -search (xside, ply + 1,
  1207.                  (depth > 0) ? depth - 1 : 0,
  1208.                  -beta, -alpha,
  1209.                  nxtline, &rcnt);
  1210.       if (abs (node->score) > 9000)
  1211.         node->flags |= exact;
  1212.       else if (rcnt == 1)
  1213.         node->score /= 2;
  1214.       if (rcnt >= 2 || GameCnt - Game50 > 99 ||
  1215.           (node->score == 9999 - ply && !ChkFlag[ply]))
  1216.         {
  1217.           node->flags |= draw;
  1218.           node->flags |= exact;
  1219.           if (side == computer)
  1220.         node->score = contempt;
  1221.           else
  1222.         node->score = -contempt;
  1223.         }
  1224.       node->reply = nxtline[ply + 1];
  1225.       UnmakeMove (side, node, &tempb, &tempc, &tempsf, &tempst);
  1226.     }
  1227.       if (node->score > best && !flag.timeout)
  1228.     {
  1229.       if (depth > 0)
  1230.         if (node->score > alpha && !(node->flags & exact))
  1231.           node->score += depth;
  1232.       best = node->score;
  1233.       pbst = pnt;
  1234.       if (best > alpha)
  1235.         alpha = best;
  1236.       for (j = ply + 1; nxtline[j] > 0; j++)
  1237.         bstline[j] = nxtline[j];
  1238.       bstline[j] = 0;
  1239.       bstline[ply] = mv;
  1240.       if (ply == 1)
  1241.         {
  1242.           if (best > root->score)
  1243.         {
  1244.           tmp = Tree[pnt];
  1245.           for (j = pnt - 1; j >= 0; j--)
  1246.             Tree[j + 1] = Tree[j];
  1247.           Tree[0] = tmp;
  1248.           pbst = 0;
  1249.         }
  1250.           if (Sdepth > 2)
  1251.         if (best > beta)
  1252.           ShowResults (best, bstline, '+');
  1253.         else if (best < alpha)
  1254.           ShowResults (best, bstline, '-');
  1255.         else
  1256.           ShowResults (best, bstline, '&');
  1257.         }
  1258.     }
  1259.       if (NodeCnt > ETnodes)
  1260.     ElapsedTime (0);
  1261.       if (flag.timeout)
  1262.     return (-Tscore[ply - 1]);
  1263.     }
  1264.  
  1265.   node = &Tree[pbst];
  1266.   mv = (node->f << 8) | node->t;
  1267. #if ttblsz
  1268.   if (flag.hash && ply <= Sdepth && *rpt == 0 && best == alpha)
  1269.     {
  1270.       PutInTTable (side, best, depth, alpha, beta, mv);
  1271. #ifdef HASHFILE      
  1272.       if (hashfile && (depth > 5) && (GameCnt < 12))
  1273.     PutInFTable (side, best, depth, alpha, beta, node->f, node->t);
  1274. #endif /* HASHFILE */      
  1275.     }
  1276. #endif /* ttblsz */
  1277.   if (depth > 0)
  1278.     {
  1279.       j = (node->f << 6) | node->t;
  1280.       if (side == black)
  1281.     j |= 0x1000;
  1282.       if (history[j] < 150)
  1283.     history[j] += (unsigned char) 2 * depth;
  1284.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  1285.     if (best <= beta)
  1286.       killr3[ply] = mv;
  1287.     else if (mv != killr1[ply])
  1288.       {
  1289.         killr2[ply] = killr1[ply];
  1290.         killr1[ply] = mv;
  1291.       }
  1292.       if (best > 9000)
  1293.     killr0[ply] = mv;
  1294.       else
  1295.     killr0[ply] = 0;
  1296.     }
  1297.   return (best);
  1298. }
  1299.  
  1300. #if ttblsz
  1301. /*
  1302.   hashbd contains a 32 bit "signature" of the board position. hashkey
  1303.   contains a 16 bit code used to address the hash table. When a move is
  1304.   made, XOR'ing the hashcode of moved piece on the from and to squares with
  1305.   the hashbd and hashkey values keeps things current.
  1306. */
  1307. #define UpdateHashbd(side, piece, f, t) \
  1308. {\
  1309.   if ((f) >= 0)\
  1310.     {\
  1311.       hashbd ^= hashcode[side][piece][f].bd;\
  1312.       hashkey ^= hashcode[side][piece][f].key;\
  1313.     }\
  1314.   if ((t) >= 0)\
  1315.     {\
  1316.       hashbd ^= hashcode[side][piece][t].bd;\
  1317.       hashkey ^= hashcode[side][piece][t].key;\
  1318.     }\
  1319. }
  1320.  
  1321. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  1322.            | (board[2 * (i)] << 4)\
  1323.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  1324.            | (board[2 * (i) + 1]))
  1325.  
  1326. int
  1327. ProbeTTable (short int side,
  1328.          short int depth,
  1329.          short int *alpha,
  1330.          short int *beta,
  1331.          short int *score)
  1332.  
  1333. /*
  1334.   Look for the current board position in the transposition table.
  1335. */
  1336.  
  1337. {
  1338.   register struct hashentry *ptbl;
  1339.   register unsigned short i;
  1340.  
  1341.   ptbl = &ttable[side][hashkey & (ttblsz - 1)];
  1342.  
  1343.   /* rehash max rehash times */
  1344.   for (i = 1; ptbl->hashbd != hashbd && i <= rehash; i++)
  1345.     ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)];
  1346.   if ((short) ptbl->depth >= depth && ptbl->hashbd == hashbd)
  1347.     {
  1348.       HashCnt++;
  1349. #ifdef HASHTEST
  1350.       for (i = 0; i < 32; i++)
  1351.     {
  1352.       if (ptbl->bd[i] != CB(i))
  1353.         {
  1354.           HashCol++;
  1355.           ShowMessage("ttable collision detected");
  1356.           break;
  1357.         }
  1358.     }
  1359. #endif /* HASHTEST */
  1360.  
  1361.       PV = ptbl->mv;
  1362.       if (ptbl->flags & truescore)
  1363.     {
  1364.       *score = ptbl->score;
  1365.       *beta = -20000;
  1366.     }
  1367. #if 0 /* commented out, why? */
  1368.       else if (ptbl->flags & upperbound)
  1369.     {
  1370.       if (ptbl->score < *beta) *beta = ptbl->score+1;
  1371.     }
  1372. #endif
  1373.       else if (ptbl->flags & lowerbound)
  1374.     {
  1375.       if (ptbl->score > *alpha)
  1376.         *alpha = ptbl->score - 1;
  1377.     }
  1378.       return(true);
  1379.     }
  1380.   return(false);
  1381. }
  1382.  
  1383. void
  1384. PutInTTable (short int side,
  1385.          short int score,
  1386.          short int depth,
  1387.          short int alpha,
  1388.          short int beta,
  1389.          short unsigned int mv)
  1390.  
  1391. /*
  1392.   Store the current board position in the transposition table.
  1393. */
  1394.  
  1395. {
  1396.   register struct hashentry *ptbl;
  1397.   register unsigned short i;
  1398.  
  1399.   ptbl = &ttable[side][hashkey & (ttblsz - 1)];
  1400.  
  1401.   /* rehash max rehash times */
  1402.   for (i = 1; depth < ptbl->depth && ptbl->hashbd != hashbd && i <= rehash; i++)
  1403.     ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)];
  1404.   if (depth >= ptbl->depth || ptbl->hashbd != hashbd)
  1405.     {
  1406.       ptbl->hashbd = hashbd;
  1407.       ptbl->depth = (unsigned char) depth;
  1408.       ptbl->score = score;
  1409.       ptbl->mv = mv;
  1410.       ptbl->flags = 0;
  1411.       if (score < alpha)
  1412.     ptbl->flags |= upperbound;
  1413.       else if (score > beta)
  1414.     ptbl->flags |= lowerbound;
  1415.       else
  1416.     ptbl->flags |= truescore;
  1417. #ifdef HASHTEST
  1418.       for (i = 0; i < 32; i++)
  1419.     {
  1420.       ptbl->bd[i] = CB(i);
  1421.     }
  1422. #endif /* HASHTEST */
  1423.     }
  1424. }
  1425.  
  1426. void
  1427. ZeroTTable (void)
  1428. {
  1429.   register int side, i;
  1430.  
  1431.   if (flag.hash)
  1432.     for (side = white; side <= black; side++)
  1433.       for (i = 0; i < ttblsz; i++)
  1434.     ttable[side][i].depth = 0;
  1435. }
  1436.  
  1437. #ifdef HASHFILE
  1438. int
  1439. ProbeFTable(short int side,
  1440.         short int depth,
  1441.         short int *alpha,
  1442.         short int *beta,
  1443.         short int *score)
  1444.  
  1445. /*
  1446.   Look for the current board position in the persistent transposition table.
  1447. */
  1448.  
  1449. {
  1450.   register unsigned short i, j;
  1451.   register unsigned long hashix;
  1452.   short s;
  1453.   struct fileentry new, t;
  1454.  
  1455.   if (side == white)
  1456.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  1457.   else
  1458.     hashix = hashkey | 1 & (filesz - 1);
  1459.  
  1460.   for (i = 0; i < 32; i++)
  1461.     new.bd[i] = CB(i);
  1462.   new.flags = 0;
  1463.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  1464.     new.flags |= queencastle;
  1465.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  1466.     new.flags |= kingcastle;
  1467.  
  1468.   for (i = 0; i < frehash; i++)
  1469.     {
  1470.       fseek(hashfile,
  1471.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1472.         SEEK_SET);
  1473.       fread(&t, sizeof(struct fileentry), 1, hashfile);
  1474.       for (j = 0; j < 32; j++)
  1475.     if (t.bd[j] != new.bd[j])
  1476.       break;
  1477.       if (((short) t.depth >= depth) && (j >= 32)
  1478.       && (new.flags == (t.flags & (kingcastle | queencastle))))
  1479.     {
  1480.       FHashCnt++;
  1481.       PV = (t.f << 8) | t.t;
  1482.       s = (t.sh << 8) | t.sl;
  1483.       if (t.flags & truescore)
  1484.         {
  1485.           *score = s;
  1486.           *beta = -20000;
  1487.         }
  1488.       else if (t.flags & lowerbound)
  1489.         {
  1490.           if (s > *alpha)
  1491.         *alpha = s - 1;
  1492.         }
  1493.       return(true);
  1494.     }
  1495.     }
  1496.   return(false);
  1497. }
  1498.  
  1499. void
  1500. PutInFTable (short int side,
  1501.          short int score,
  1502.          short int depth,
  1503.          short int alpha,
  1504.          short int beta,
  1505.          short unsigned int f,
  1506.          short unsigned int t)
  1507.  
  1508. /*
  1509.   Store the current board position in the persistent transposition table.
  1510. */
  1511.  
  1512. {
  1513.   register unsigned short i;
  1514.   register unsigned long hashix;
  1515.   struct fileentry new, tmp;
  1516.  
  1517.   if (side == white)
  1518.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  1519.   else
  1520.     hashix = hashkey | 1 & (filesz - 1);
  1521.  
  1522.   for (i = 0; i < 32; i++)
  1523.     new.bd[i] = CB(i);
  1524.   new.f = (unsigned char) f;
  1525.   new.t = (unsigned char) t;
  1526.   new.flags = 0;
  1527.   if (score < alpha)
  1528.     new.flags |= upperbound;
  1529.   else if (score > beta)
  1530.     new.flags |= lowerbound;
  1531.   else
  1532.     new.flags |= truescore;
  1533.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  1534.     new.flags |= queencastle;
  1535.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  1536.     new.flags |= kingcastle;
  1537.   new.depth = (unsigned char) depth;
  1538.   new.sh = (unsigned char) (score >> 8);
  1539.   new.sl = (unsigned char) (score & 0xFF);
  1540.  
  1541.   for (i = 0; i < frehash; i++)
  1542.     {
  1543.       fseek(hashfile,
  1544.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1545.         SEEK_SET);
  1546.       fread(&tmp, sizeof(struct fileentry), 1, hashfile);
  1547.       if ((short) tmp.depth <= depth)
  1548.     {
  1549.       fseek(hashfile,
  1550.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1551.         SEEK_SET);
  1552.       fwrite (&new, sizeof(struct fileentry), 1, hashfile);
  1553.       break;
  1554.     }
  1555.     }
  1556. }
  1557. #endif /* HASHFILE */
  1558. #endif /* ttblsz */
  1559.  
  1560. void
  1561. ZeroRPT (void)
  1562. {
  1563.   register int side, i;
  1564.  
  1565.   for (side = white; side <= black; side++)
  1566.     for (i = 0; i < 256; i++)
  1567.       rpthash[side][i] = 0;
  1568. }
  1569.  
  1570. #define Link(from,to,flag,s) \
  1571. {\
  1572.    node->f = from; node->t = to;\
  1573.      node->reply = 0;\
  1574.        node->flags = flag;\
  1575.      node->score = s;\
  1576.        ++node;\
  1577.          ++TrPnt[ply+1];\
  1578.          }
  1579.  
  1580. static inline void
  1581. LinkMove (short int ply,
  1582.       short int f,
  1583.       short int t,
  1584.       short int flag,
  1585.       short int xside)
  1586.  
  1587. /*
  1588.   Add a move to the tree.  Assign a bonus to order the moves
  1589.   as follows:
  1590.   1. Principle variation
  1591.   2. Capture of last moved piece
  1592.   3. Other captures (major pieces first)
  1593.   4. Killer moves
  1594.   5. "history" killers
  1595. */
  1596.  
  1597. {
  1598.   register short s, z;
  1599.   register unsigned short mv;
  1600.   register struct leaf *node;
  1601.  
  1602.   node = &Tree[TrPnt[ply + 1]];
  1603.   mv = (f << 8) | t;
  1604.   s = 0;
  1605.   if (mv == Swag0)
  1606.     s = 2000;
  1607.   else if (mv == Swag1)
  1608.     s = 60;
  1609.   else if (mv == Swag2)
  1610.     s = 50;
  1611.   else if (mv == Swag3)
  1612.     s = 40;
  1613.   else if (mv == Swag4)
  1614.     s = 30;
  1615.   z = (f << 6) | t;
  1616.   if (xside == white)
  1617.     z |= 0x1000;
  1618.   s += history[z];
  1619.   if (color[t] != neutral)
  1620.     {
  1621.       if (t == TOsquare)
  1622.     s += 500;
  1623.       s += value[board[t]] - board[f];
  1624.     }
  1625.   if (board[f] == pawn)
  1626.     if (row (t) == 0 || row (t) == 7)
  1627.       {
  1628.     flag |= promote;
  1629.     s += 800;
  1630.     Link (f, t, flag | queen, s - 20000);
  1631.     s -= 200;
  1632.     Link (f, t, flag | knight, s - 20000);
  1633.     s -= 50;
  1634.     Link (f, t, flag | rook, s - 20000);
  1635.     flag |= bishop;
  1636.     s -= 50;
  1637.       }
  1638.     else if (row (t) == 1 || row (t) == 6)
  1639.       {
  1640.     flag |= pwnthrt;
  1641.     s += 600;
  1642.       }
  1643.   Link (f, t, flag, s - 20000);
  1644. }
  1645.  
  1646.  
  1647. static inline void
  1648. GenMoves (short int ply, short int sq, short int side, short int xside)
  1649.  
  1650. /*
  1651.   Generate moves for a piece. The moves are taken from the precalulated
  1652.   array nextpos/nextdir. If the board is free, next move is choosen from
  1653.   nextpos else from nextdir.
  1654. */
  1655.  
  1656. {
  1657.   register short u, piece;
  1658.   register unsigned char *ppos, *pdir;
  1659.  
  1660.   piece = board[sq];
  1661.   ppos = nextpos[ptype[side][piece]][sq];
  1662.   pdir = nextdir[ptype[side][piece]][sq];
  1663.   if (piece == pawn)
  1664.     {
  1665.       u = ppos[sq];    /* follow no captures thread */
  1666.       if (color[u] == neutral)
  1667.     {
  1668.       LinkMove (ply, sq, u, 0, xside);
  1669.       u = ppos[u];
  1670.       if (color[u] == neutral)
  1671.         LinkMove (ply, sq, u, 0, xside);
  1672.     }
  1673.       u = pdir[sq];    /* follow captures thread */
  1674.       if (color[u] == xside)
  1675.     LinkMove (ply, sq, u, capture, xside);
  1676.       else
  1677.     if (u == epsquare)
  1678.       LinkMove (ply, sq, u, capture | epmask, xside);
  1679.       u = pdir[u];
  1680.       if (color[u] == xside)
  1681.     LinkMove (ply, sq, u, capture, xside);
  1682.       else
  1683.     if (u == epsquare)
  1684.       LinkMove (ply, sq, u, capture | epmask, xside);
  1685.     }
  1686.   else
  1687.     {
  1688.       u = ppos[sq];
  1689.       do
  1690.     {
  1691.       if (color[u] == neutral)
  1692.         {
  1693.           LinkMove (ply, sq, u, 0, xside);
  1694.           u = ppos[u];
  1695.         }
  1696.       else
  1697.         {
  1698.           if (color[u] == xside)
  1699.         LinkMove (ply, sq, u, capture, xside);
  1700.           u = pdir[u];
  1701.         }
  1702.       } while (u != sq);
  1703.     }
  1704. }
  1705.  
  1706. void
  1707. MoveList (short int side, short int ply)
  1708.  
  1709. /*
  1710.   Fill the array Tree[] with all available moves for side to play. Array
  1711.   TrPnt[ply] contains the index into Tree[] of the first move at a ply.
  1712. */
  1713.  
  1714. {
  1715.   register short i, xside, f;
  1716.  
  1717.   xside = otherside[side];
  1718.   TrPnt[ply + 1] = TrPnt[ply];
  1719.   if (PV == 0)
  1720.     Swag0 = killr0[ply];
  1721.   else
  1722.     Swag0 = PV;
  1723.   Swag1 = killr1[ply];
  1724.   Swag2 = killr2[ply];
  1725.   Swag3 = killr3[ply];
  1726.   if (ply > 2)
  1727.     Swag4 = killr1[ply - 2];
  1728.   else
  1729.     Swag4 = 0;
  1730.   for (i = PieceCnt[side]; i >= 0; i--)
  1731.     GenMoves (ply, PieceList[side][i], side, xside);
  1732.   if (!castld[side])
  1733.     {
  1734.       f = PieceList[side][0];
  1735.       if (castle (side, f, f + 2, 0))
  1736.     {
  1737.       LinkMove (ply, f, f + 2, cstlmask, xside);
  1738.     }
  1739.       if (castle (side, f, f - 2, 0))
  1740.     {
  1741.       LinkMove (ply, f, f - 2, cstlmask, xside);
  1742.     }
  1743.     }
  1744. }
  1745.  
  1746. void
  1747. CaptureList (short int side, short int ply)
  1748.  
  1749. /*
  1750.   Fill the array Tree[] with all available cature and promote moves for
  1751.   side to play. Array TrPnt[ply] contains the index into Tree[]
  1752.   of the first move at a ply.
  1753. */
  1754.  
  1755. {
  1756.   register short u, sq, xside;
  1757.   register struct leaf *node;
  1758.   register unsigned char *ppos, *pdir;
  1759.   short i, piece, *PL, r7;
  1760.  
  1761.   xside = otherside[side];
  1762.   TrPnt[ply + 1] = TrPnt[ply];
  1763.   node = &Tree[TrPnt[ply]];
  1764.   r7 = rank7[side];
  1765.   PL = PieceList[side];
  1766.   for (i = 0; i <= PieceCnt[side]; i++)
  1767.     {
  1768.       sq = PL[i];
  1769.       piece = board[sq];
  1770.       if (sweep[piece])
  1771.     {
  1772.       ppos = nextpos[piece][sq];
  1773.       pdir = nextdir[piece][sq];
  1774.       u = ppos[sq];
  1775.       do
  1776.         {
  1777.           if (color[u] == neutral)
  1778.         u = ppos[u];
  1779.           else
  1780.         {
  1781.           if (color[u] == xside)
  1782.             Link (sq, u, capture,
  1783.               value[board[u]] + svalue[board[u]] - piece);
  1784.           u = pdir[u];
  1785.         }
  1786.       } while (u != sq);
  1787.     }
  1788.       else
  1789.     {
  1790.       pdir = nextdir[ptype[side][piece]][sq];
  1791.       if (piece == pawn && row (sq) == r7)
  1792.         {
  1793.           u = pdir[sq];
  1794.           if (color[u] == xside)
  1795.         Link (sq, u, capture | promote | queen, valueQ);
  1796.           u = pdir[u];
  1797.           if (color[u] == xside)
  1798.         Link (sq, u, capture | promote | queen, valueQ);
  1799.           ppos = nextpos[ptype[side][piece]][sq];
  1800.           u = ppos[sq]; /* also generate non capture promote */
  1801.           if (color[u] == neutral)
  1802.         Link (sq, u, promote | queen, valueQ);
  1803.         }
  1804.       else
  1805.         {
  1806.           u = pdir[sq];
  1807.           do
  1808.         {
  1809.           if (color[u] == xside)
  1810.             Link (sq, u, capture,
  1811.               value[board[u]] + svalue[board[u]] - piece);
  1812.           u = pdir[u];
  1813.           } while (u != sq);
  1814.         }
  1815.     }
  1816.     }
  1817. }
  1818.  
  1819.  
  1820. int
  1821. castle (short int side, short int kf, short int kt, short int iop)
  1822.  
  1823. /* Make or Unmake a castling move. */
  1824.  
  1825. {
  1826.   register short rf, rt, t0, xside;
  1827.  
  1828.   xside = otherside[side];
  1829.   if (kt > kf)
  1830.     {
  1831.       rf = kf + 3;
  1832.       rt = kt - 1;
  1833.     }
  1834.   else
  1835.     {
  1836.       rf = kf - 4;
  1837.       rt = kt + 1;
  1838.     }
  1839.   if (iop == 0)
  1840.     {
  1841.       if (kf != kingP[side] ||
  1842.       board[kf] != king ||
  1843.       board[rf] != rook ||
  1844.       Mvboard[kf] != 0 ||
  1845.       Mvboard[rf] != 0 ||
  1846.       color[kt] != neutral ||
  1847.       color[rt] != neutral ||
  1848.       color[kt - 1] != neutral ||
  1849.       SqAtakd (kf, xside) ||
  1850.       SqAtakd (kt, xside) ||
  1851.       SqAtakd (rt, xside))
  1852.     return (false);
  1853.     }
  1854.   else
  1855.     {
  1856.       if (iop == 1)
  1857.     {
  1858.       castld[side] = true;
  1859.       Mvboard[kf]++;
  1860.       Mvboard[rf]++;
  1861.     }
  1862.       else
  1863.     {
  1864.       castld[side] = false;
  1865.       Mvboard[kf]--;
  1866.       Mvboard[rf]--;
  1867.       t0 = kt;
  1868.       kt = kf;
  1869.       kf = t0;
  1870.       t0 = rt;
  1871.       rt = rf;
  1872.       rf = t0;
  1873.     }
  1874.       board[kt] = king;
  1875.       color[kt] = side;
  1876.       Pindex[kt] = 0;
  1877.       board[kf] = no_piece;
  1878.       color[kf] = neutral;
  1879.       board[rt] = rook;
  1880.       color[rt] = side;
  1881.       Pindex[rt] = Pindex[rf];
  1882.       board[rf] = no_piece;
  1883.       color[rf] = neutral;
  1884.       PieceList[side][Pindex[kt]] = kt;
  1885.       PieceList[side][Pindex[rt]] = rt;
  1886. #if ttblsz
  1887.       UpdateHashbd (side, king, kf, kt);
  1888.       UpdateHashbd (side, rook, rf, rt);
  1889. #endif /* ttblsz */
  1890.     }
  1891.   return (true);
  1892. }
  1893.  
  1894.  
  1895. static inline void
  1896. EnPassant (short int xside, short int f, short int t, short int iop)
  1897.  
  1898. /*
  1899.   Make or unmake an en passant move.
  1900. */
  1901.  
  1902. {
  1903.   register short l;
  1904.  
  1905.   if (t > f)
  1906.     l = t - 8;
  1907.   else
  1908.     l = t + 8;
  1909.   if (iop == 1)
  1910.     {
  1911.       board[l] = no_piece;
  1912.       color[l] = neutral;
  1913.     }
  1914.   else
  1915.     {
  1916.       board[l] = pawn;
  1917.       color[l] = xside;
  1918.     }
  1919.   InitializeStats ();
  1920. }
  1921.  
  1922.  
  1923. static inline void
  1924. UpdatePieceList (short int side, short int sq, short int iop)
  1925.  
  1926. /*
  1927.   Update the PieceList and Pindex arrays when a piece is captured or when a
  1928.   capture is unmade.
  1929. */
  1930.  
  1931. {
  1932.   register short i;
  1933.   if (iop == 1)
  1934.     {
  1935.       PieceCnt[side]--;
  1936.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1937.     {
  1938.       PieceList[side][i] = PieceList[side][i + 1];
  1939.       Pindex[PieceList[side][i]] = i;
  1940.     }
  1941.     }
  1942.   else
  1943.     {
  1944.       PieceCnt[side]++;
  1945.       PieceList[side][PieceCnt[side]] = sq;
  1946.       Pindex[sq] = PieceCnt[side];
  1947.     }
  1948. }
  1949.  
  1950. void
  1951. MakeMove (short int side,
  1952.       struct leaf * node,
  1953.       short int *tempb,
  1954.       short int *tempc,
  1955.       short int *tempsf,
  1956.       short int *tempst,
  1957.       short int *INCscore)
  1958.  
  1959. /*
  1960.   Update Arrays board[], color[], and Pindex[] to reflect the new board
  1961.   position obtained after making the move pointed to by node. Also update
  1962.   miscellaneous stuff that changes when a move is made.
  1963. */
  1964.  
  1965. {
  1966.   register short f, t, xside, ct, cf;
  1967.  
  1968.   xside = otherside[side];
  1969.   GameCnt++;
  1970.   f = node->f;
  1971.   t = node->t;
  1972.   epsquare = -1;
  1973.   FROMsquare = f;
  1974.   TOsquare = t;
  1975.   *INCscore = 0;
  1976.   GameList[GameCnt].gmove = (f << 8) | t;
  1977.   if (node->flags & cstlmask)
  1978.     {
  1979.       GameList[GameCnt].piece = no_piece;
  1980.       GameList[GameCnt].color = side;
  1981.       (void) castle (side, f, t, 1);
  1982.     }
  1983.   else
  1984.     {
  1985.       if (!(node->flags & capture) && (board[f] != pawn))
  1986.         rpthash[side][hashkey & 0xFF]++;
  1987.       *tempc = color[t];
  1988.       *tempb = board[t];
  1989.       *tempsf = svalue[f];
  1990.       *tempst = svalue[t];
  1991.       GameList[GameCnt].piece = *tempb;
  1992.       GameList[GameCnt].color = *tempc;
  1993.       if (*tempc != neutral)
  1994.     {
  1995.       UpdatePieceList (*tempc, t, 1);
  1996.       if (*tempb == pawn)
  1997.         --PawnCnt[*tempc][column (t)];
  1998.       if (board[f] == pawn)
  1999.         {
  2000.           --PawnCnt[side][column (f)];
  2001.           ++PawnCnt[side][column (t)];
  2002.           cf = column (f);
  2003.           ct = column (t);
  2004.           if (PawnCnt[side][ct] > 1 + PawnCnt[side][cf])
  2005.         *INCscore -= 15;
  2006.           else if (PawnCnt[side][ct] < 1 + PawnCnt[side][cf])
  2007.         *INCscore += 15;
  2008.           else if (ct == 0 || ct == 7 || PawnCnt[side][ct + ct - cf] == 0)
  2009.         *INCscore -= 15;
  2010.         }
  2011.       mtl[xside] -= value[*tempb];
  2012.       if (*tempb == pawn)
  2013.         pmtl[xside] -= valueP;
  2014. #if ttblsz
  2015.       UpdateHashbd (xside, *tempb, -1, t);
  2016. #endif /* ttblsz */
  2017.       *INCscore += *tempst;
  2018.       Mvboard[t]++;
  2019.     }
  2020.       color[t] = color[f];
  2021.       board[t] = board[f];
  2022.       svalue[t] = svalue[f];
  2023.       Pindex[t] = Pindex[f];
  2024.       PieceList[side][Pindex[t]] = t;
  2025.       color[f] = neutral;
  2026.       board[f] = no_piece;
  2027.       if (board[t] == pawn)
  2028.     if (t - f == 16)
  2029.       epsquare = f + 8;
  2030.     else if (f - t == 16)
  2031.       epsquare = f - 8;
  2032.       if (node->flags & promote)
  2033.     {
  2034.       board[t] = node->flags & pmask;
  2035.       if (board[t] == queen)
  2036.         HasQueen[side]++;
  2037.       else if (board[t] == rook)
  2038.         HasRook[side]++;
  2039.       else if (board[t] == bishop)
  2040.         HasBishop[side]++;
  2041.       else if (board[t] == knight)
  2042.         HasKnight[side]++;
  2043.       --PawnCnt[side][column (t)];
  2044.       mtl[side] += value[board[t]] - valueP;
  2045.       pmtl[side] -= valueP;
  2046. #if ttblsz
  2047.       UpdateHashbd (side, pawn, f, -1);
  2048.       UpdateHashbd (side, board[t], f, -1);
  2049. #endif /* ttblsz */
  2050.       *INCscore -= *tempsf;
  2051.     }
  2052.       if (node->flags & epmask)
  2053.     EnPassant (xside, f, t, 1);
  2054.       else
  2055. #if ttblsz
  2056.     UpdateHashbd (side, board[t], f, t);
  2057. #else
  2058.     /* NOOP */;        
  2059. #endif /* ttblsz */
  2060.       Mvboard[f]++;
  2061.     }
  2062. }
  2063.  
  2064. void
  2065. UnmakeMove (short int side,
  2066.         struct leaf * node,
  2067.         short int *tempb,
  2068.         short int *tempc,
  2069.         short int *tempsf,
  2070.         short int *tempst)
  2071.  
  2072. /*
  2073.   Take back a move.
  2074. */
  2075.  
  2076. {
  2077.   register short f, t, xside;
  2078.  
  2079.   xside = otherside[side];
  2080.   f = node->f;
  2081.   t = node->t;
  2082.   epsquare = -1;
  2083.   GameCnt--;
  2084.   if (node->flags & cstlmask)
  2085.     (void) castle (side, f, t, 2);
  2086.   else
  2087.     {
  2088.       color[f] = color[t];
  2089.       board[f] = board[t];
  2090.       svalue[f] = *tempsf;
  2091.       Pindex[f] = Pindex[t];
  2092.       PieceList[side][Pindex[f]] = f;
  2093.       color[t] = *tempc;
  2094.       board[t] = *tempb;
  2095.       svalue[t] = *tempst;
  2096.       if (node->flags & promote)
  2097.     {
  2098.       board[f] = pawn;
  2099.       ++PawnCnt[side][column (t)];
  2100.       mtl[side] += valueP - value[node->flags & pmask];
  2101.       pmtl[side] += valueP;
  2102. #if ttblsz
  2103.       UpdateHashbd (side, (short) node->flags & pmask, -1, t);
  2104.       UpdateHashbd (side, pawn, -1, t);
  2105. #endif /* ttblsz */
  2106.     }
  2107.       if (*tempc != neutral)
  2108.     {
  2109.       UpdatePieceList (*tempc, t, 2);
  2110.       if (*tempb == pawn)
  2111.         ++PawnCnt[*tempc][column (t)];
  2112.       if (board[f] == pawn)
  2113.         {
  2114.           --PawnCnt[side][column (t)];
  2115.           ++PawnCnt[side][column (f)];
  2116.         }
  2117.       mtl[xside] += value[*tempb];
  2118.       if (*tempb == pawn)
  2119.         pmtl[xside] += valueP;
  2120. #if ttblsz
  2121.       UpdateHashbd (xside, *tempb, -1, t);
  2122. #endif /* ttblsz */
  2123.       Mvboard[t]--;
  2124.     }
  2125.       if (node->flags & epmask)
  2126.     EnPassant (xside, f, t, 2);
  2127.       else
  2128. #if ttblsz
  2129.     UpdateHashbd (side, board[f], f, t);
  2130. #else
  2131.       /* NOOP */;
  2132. #endif /* ttblsz */
  2133.       Mvboard[f]--;
  2134.       if (!(node->flags & capture) && (board[f] != pawn))
  2135.         rpthash[side][hashkey & 0xFF]--;
  2136.     }
  2137. }
  2138.  
  2139.  
  2140. void
  2141. InitializeStats (void)
  2142.  
  2143. /*
  2144.   Scan thru the board seeing what's on each square. If a piece is found,
  2145.   update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also
  2146.   determine the material for each side and set the hashkey and hashbd
  2147.   variables to represent the current board position. Array
  2148.   PieceList[side][indx] contains the location of all the pieces of either
  2149.   side. Array Pindex[sq] contains the indx into PieceList for a given
  2150.   square.
  2151. */
  2152.  
  2153. {
  2154.   register short i, sq;
  2155.   epsquare = -1;
  2156.   for (i = 0; i < 8; i++)
  2157.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  2158.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  2159.   PieceCnt[white] = PieceCnt[black] = 0;
  2160. #if ttblsz
  2161.   hashbd = hashkey = 0;
  2162. #endif /* ttblsz */
  2163.   for (sq = 0; sq < 64; sq++)
  2164.     if (color[sq] != neutral)
  2165.       {
  2166.     mtl[color[sq]] += value[board[sq]];
  2167.     if (board[sq] == pawn)
  2168.       {
  2169.         pmtl[color[sq]] += valueP;
  2170.         ++PawnCnt[color[sq]][column (sq)];
  2171.       }
  2172.     if (board[sq] == king)
  2173.       Pindex[sq] = 0;
  2174.     else
  2175.       Pindex[sq] = ++PieceCnt[color[sq]];
  2176.     PieceList[color[sq]][Pindex[sq]] = sq;
  2177. #if ttblsz
  2178.     hashbd ^= hashcode[color[sq]][board[sq]][sq].bd;
  2179.     hashkey ^= hashcode[color[sq]][board[sq]][sq].key;
  2180. #endif /* ttblsz */
  2181.       }
  2182. }
  2183.  
  2184.  
  2185. int
  2186. SqAtakd (short int sq, short int side)
  2187.  
  2188. /*
  2189.   See if any piece with color 'side' ataks sq.  First check pawns then Queen,
  2190.   Bishop, Rook and King and last Knight.
  2191. */
  2192.  
  2193. {
  2194.   register short u;
  2195.   register unsigned char *ppos, *pdir;
  2196.   short xside;
  2197.  
  2198.   xside = otherside[side];
  2199.   pdir = nextdir[ptype[xside][pawn]][sq];
  2200.   u = pdir[sq];        /* follow captures thread */
  2201.   if (u != sq)
  2202.     {
  2203.       if (board[u] == pawn && color[u] == side)
  2204.     return (true);
  2205.       u = pdir[u];
  2206.       if (u != sq && board[u] == pawn && color[u] == side)
  2207.     return (true);
  2208.     }
  2209.   /* king capture */
  2210.   if (distance (sq, PieceList[side][0]) == 1)
  2211.     return (true);
  2212.   /* try a queen bishop capture */
  2213.   ppos = nextpos[bishop][sq];
  2214.   pdir = nextdir[bishop][sq];
  2215.   u = ppos[sq];
  2216.   do
  2217.     {
  2218.       if (color[u] == neutral)
  2219.     u = ppos[u];
  2220.       else
  2221.     {
  2222.       if (color[u] == side &&
  2223.           (board[u] == queen || board[u] == bishop))
  2224.         return (true);
  2225.       u = pdir[u];
  2226.     }
  2227.   } while (u != sq);
  2228.   /* try a queen rook capture */
  2229.   ppos = nextpos[rook][sq];
  2230.   pdir = nextdir[rook][sq];
  2231.   u = ppos[sq];
  2232.   do
  2233.     {
  2234.       if (color[u] == neutral)
  2235.     u = ppos[u];
  2236.       else
  2237.     {
  2238.       if (color[u] == side &&
  2239.           (board[u] == queen || board[u] == rook))
  2240.         return (true);
  2241.       u = pdir[u];
  2242.     }
  2243.   } while (u != sq);
  2244.   /* try a knight capture */
  2245.   pdir = nextdir[knight][sq];
  2246.   u = pdir[sq];
  2247.   do
  2248.     {
  2249.       if (color[u] == side && board[u] == knight)
  2250.     return (true);
  2251.       u = pdir[u];
  2252.   } while (u != sq);
  2253.   return (false);
  2254. }
  2255.  
  2256. static inline void
  2257. ataks (short int side, short int *a)
  2258.  
  2259. /*
  2260.   Fill array atak[][] with info about ataks to a square.  Bits 8-15 are set
  2261.   if the piece (king..pawn) ataks the square.  Bits 0-7 contain a count of
  2262.   total ataks to the square.
  2263. */
  2264.  
  2265. {
  2266.   register short u, c, sq;
  2267.   register unsigned char *ppos, *pdir;
  2268.   short i, piece, *PL;
  2269.  
  2270. #ifdef NOMEMSET
  2271.   for (u = 64; u; a[--u] = 0) ;
  2272. #else
  2273.   memset ((char *) a, 0, 64 * sizeof (a[0]));
  2274. #endif /* NOMEMSET */
  2275.   PL = PieceList[side];
  2276.   for (i = PieceCnt[side]; i >= 0; i--)
  2277.     {
  2278.       sq = PL[i];
  2279.       piece = board[sq];
  2280.       c = control[piece];
  2281.       if (sweep[piece])
  2282.     {
  2283.       ppos = nextpos[piece][sq];
  2284.       pdir = nextdir[piece][sq];
  2285.       u = ppos[sq];
  2286.       do
  2287.         {
  2288.           a[u] = ++a[u] | c;
  2289.           u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2290.       } while (u != sq);
  2291.     }
  2292.       else
  2293.     {
  2294.       pdir = nextdir[ptype[side][piece]][sq];
  2295.       u = pdir[sq];    /* follow captures thread for pawns */
  2296.       do
  2297.         {
  2298.           a[u] = ++a[u] | c;
  2299.           u = pdir[u];
  2300.       } while (u != sq);
  2301.     }
  2302.     }
  2303. }
  2304.  
  2305.  
  2306. /* ............    POSITIONAL EVALUATION ROUTINES    ............ */
  2307.  
  2308. int
  2309. evaluate (short int side,
  2310.       short int ply,
  2311.       short int alpha,
  2312.       short int beta,
  2313.       short int INCscore,
  2314.       short int *slk,
  2315.       short int *InChk)
  2316.  
  2317. /*
  2318.   Compute an estimate of the score by adding the positional score from the
  2319.   previous ply to the material difference. If this score falls inside a
  2320.   window which is 180 points wider than the alpha-beta window (or within a
  2321.   50 point window during quiescence search) call ScorePosition() to
  2322.   determine a score, otherwise return the estimated score. If one side has
  2323.   only a king and the other either has no pawns or no pieces then the
  2324.   function ScoreLoneKing() is called.
  2325. */
  2326.  
  2327. {
  2328.   register short evflag, xside;
  2329.   short s;
  2330.  
  2331.   xside = otherside[side];
  2332.   s = -Pscore[ply - 1] + mtl[side] - mtl[xside] - INCscore;
  2333.   hung[white] = hung[black] = 0;
  2334.   *slk = ((mtl[white] == valueK && (pmtl[black] == 0 || emtl[black] == 0)) ||
  2335.      (mtl[black] == valueK && (pmtl[white] == 0 || emtl[white] == 0)));
  2336.  
  2337.   if (*slk)
  2338.     evflag = false;
  2339.   else
  2340.     evflag =
  2341.       (ply == 1 || ply < Sdepth ||
  2342.        ((ply == Sdepth + 1 || ply == Sdepth + 2) &&
  2343.     (s > alpha - xwndw && s < beta + xwndw)) ||
  2344.        (ply > Sdepth + 2 && s >= alpha - 25 && s <= beta + 25));
  2345.  
  2346.   if (evflag)
  2347.     {
  2348.       EvalNodes++;
  2349.       ataks (side, atak[side]);
  2350.       if (Anyatak (side, PieceList[xside][0]))
  2351.     return (10001 - ply);
  2352.       ataks (xside, atak[xside]);
  2353.       *InChk = Anyatak (xside, PieceList[side][0]);
  2354.       ScorePosition (side, &s);
  2355.     }
  2356.   else
  2357.     {
  2358.       if (SqAtakd (PieceList[xside][0], side))
  2359.     return (10001 - ply);
  2360.       *InChk = SqAtakd (PieceList[side][0], xside);
  2361.       if (*slk)
  2362.     ScoreLoneKing (side, &s);
  2363.     }
  2364.  
  2365.   Pscore[ply] = s - mtl[side] + mtl[xside];
  2366.   if (*InChk)
  2367.     ChkFlag[ply - 1] = Pindex[TOsquare];
  2368.   else
  2369.     ChkFlag[ply - 1] = 0;
  2370.   return (s);
  2371. }
  2372.  
  2373.  
  2374. static inline int
  2375. ScoreKPK (short int side,
  2376.       short int winner,
  2377.       short int loser,
  2378.       short int king1,
  2379.       short int king2,
  2380.       short int sq)
  2381.  
  2382. /*
  2383.   Score King and Pawns versus King endings.
  2384. */
  2385.  
  2386. {
  2387.   register short s, r;
  2388.  
  2389.   if (PieceCnt[winner] == 1)
  2390.     s = 50;
  2391.   else
  2392.     s = 120;
  2393.   if (winner == white)
  2394.     {
  2395.       if (side == loser)
  2396.     r = row (sq) - 1;
  2397.       else
  2398.     r = row (sq);
  2399.       if (row (king2) >= r && distance (sq, king2) < 8 - r)
  2400.     s += 10 * row (sq);
  2401.       else
  2402.     s = 500 + 50 * row (sq);
  2403.       if (row (sq) < 6)
  2404.     sq += 16;
  2405.       else
  2406.     if (row(sq) == 6)
  2407.       sq += 8;
  2408.     }
  2409.   else
  2410.     {
  2411.       if (side == loser)
  2412.     r = row (sq) + 1;
  2413.       else
  2414.     r = row (sq);
  2415.       if (row (king2) <= r && distance (sq, king2) < r + 1)
  2416.     s += 10 * (7 - row (sq));
  2417.       else
  2418.     s = 500 + 50 * (7 - row (sq));
  2419.       if (row (sq) > 1)
  2420.     sq -= 16;
  2421.       else
  2422.     if (row(sq) == 1)
  2423.       sq -= 8;
  2424.     }
  2425.   s += 8 * (taxicab (king2, sq) - taxicab (king1, sq));
  2426.   return (s);
  2427. }
  2428.  
  2429.  
  2430. static inline int
  2431. ScoreKBNK (short int winner, short int king1, short int king2)
  2432.  
  2433.  
  2434. /*
  2435.   Score King+Bishop+Knight versus King endings.
  2436.   This doesn't work all that well but it's better than nothing.
  2437. */
  2438.  
  2439. {
  2440.   register short s, sq, KBNKsq = 0;
  2441.  
  2442.   for (sq = 0; sq < 64; sq++)
  2443.     if (board[sq] == bishop)
  2444.       if (row (sq) % 2 == column (sq) % 2)
  2445.     KBNKsq = 0;
  2446.       else
  2447.     KBNKsq = 7;
  2448.  
  2449.   s = emtl[winner] - 300;
  2450.   if (KBNKsq == 0)
  2451.     s += KBNK[king2];
  2452.   else
  2453.     s += KBNK[locn (row (king2), 7 - column (king2))];
  2454.   s -= taxicab (king1, king2);
  2455.   s -= distance (PieceList[winner][1], king2);
  2456.   s -= distance (PieceList[winner][2], king2);
  2457.   return (s);
  2458. }
  2459.  
  2460.  
  2461. void
  2462. ScoreLoneKing (short int side, short int *score)
  2463.  
  2464. /*
  2465.   Static evaluation when loser has only a king and winner has no pawns or no
  2466.   pieces.
  2467. */
  2468.  
  2469. {
  2470.   register short winner, loser, king1, king2, s, i;
  2471.  
  2472.   UpdateWeights ();
  2473.   if (mtl[white] > mtl[black])
  2474.     winner = white;
  2475.   else
  2476.     winner = black;
  2477.   loser = otherside[winner];
  2478.   king1 = PieceList[winner][0];
  2479.   king2 = PieceList[loser][0];
  2480.  
  2481.   s = 0;
  2482.  
  2483.   if (pmtl[winner] > 0)
  2484.     for (i = 1; i <= PieceCnt[winner]; i++)
  2485.       s += ScoreKPK (side, winner, loser, king1, king2, PieceList[winner][i]);
  2486.  
  2487.   else if (emtl[winner] == valueB + valueN)
  2488.     s = ScoreKBNK (winner, king1, king2);
  2489.  
  2490.   else if (emtl[winner] > valueB)
  2491.     s = 500 + emtl[winner] - DyingKing[king2] - 2 * distance (king1, king2);
  2492.  
  2493.   if (side == winner)
  2494.     *score = s;
  2495.   else
  2496.     *score = -s;
  2497. }
  2498.  
  2499.  
  2500. static inline void
  2501. BRscan (short int sq, short int *s, short int *mob)
  2502.  
  2503. /*
  2504.   Find Bishop and Rook mobility, XRAY attacks, and pins. Increment the
  2505.   hung[] array if a pin is found.
  2506. */
  2507. {
  2508.   register short u, piece, pin;
  2509.   register unsigned char *ppos, *pdir;
  2510.   short *Kf;
  2511.  
  2512.   Kf = Kfield[c1];
  2513.   *mob = 0;
  2514.   piece = board[sq];
  2515.   ppos = nextpos[piece][sq];
  2516.   pdir = nextdir[piece][sq];
  2517.   u = ppos[sq];
  2518.   pin = -1;            /* start new direction */
  2519.   do
  2520.     {
  2521.       *s += Kf[u];
  2522.       if (color[u] == neutral)
  2523.     {
  2524.       (*mob)++;
  2525.       if (ppos[u] == pdir[u])
  2526.         pin = -1;        /* oops new direction */
  2527.       u = ppos[u];
  2528.     }
  2529.       else if (pin < 0)
  2530.     {
  2531.       if (board[u] == pawn || board[u] == king)
  2532.         u = pdir[u];
  2533.       else
  2534.         {
  2535.           if (ppos[u] != pdir[u])
  2536.         pin = u;    /* not on the edge and on to find a pin */
  2537.           u = ppos[u];
  2538.         }
  2539.     }
  2540.       else
  2541.     {
  2542.       if (color[u] == c2 && (board[u] > piece || atk2[u] == 0))
  2543.         {
  2544.           if (color[pin] == c2)
  2545.         {
  2546.           *s += PINVAL;
  2547.           if (atk2[pin] == 0 ||
  2548.               atk1[pin] > control[board[pin]] + 1)
  2549.             ++hung[c2];
  2550.         }
  2551.           else
  2552.         *s += XRAY;
  2553.         }
  2554.       pin = -1;        /* new direction */
  2555.       u = pdir[u];
  2556.     }
  2557.   } while (u != sq);
  2558. }
  2559.  
  2560.  
  2561. static inline void
  2562. KingScan (short int sq, short int *s)
  2563.  
  2564. /*
  2565.   Assign penalties if king can be threatened by checks, if squares
  2566.   near the king are controlled by the enemy (especially the queen),
  2567.   or if there are no pawns near the king.
  2568.   The following must be true:
  2569.   board[sq] == king
  2570.   c1 == color[sq]
  2571.   c2 == otherside[c1]
  2572. */
  2573.  
  2574. #define ScoreThreat \
  2575. if (color[u] != c2)\
  2576.   if (atk1[u] == 0 || (atk2[u] & 0xFF) > 1) ++cnt;\
  2577.   else *s -= 3
  2578.  
  2579. {
  2580.   register short u;
  2581.   register unsigned char *ppos, *pdir;
  2582.   register short cnt, ok;
  2583.  
  2584.   cnt = 0;
  2585.   if (HasBishop[c2] || HasQueen[c2])
  2586.     {
  2587.       ppos = nextpos[bishop][sq];
  2588.       pdir = nextdir[bishop][sq];
  2589.       u = ppos[sq];
  2590.       do
  2591.     {
  2592.       if (atk2[u] & ctlBQ)
  2593.         ScoreThreat;
  2594.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2595.       } while (u != sq);
  2596.     }
  2597.   if (HasRook[c2] || HasQueen[c2])
  2598.     {
  2599.       ppos = nextpos[rook][sq];
  2600.       pdir = nextdir[rook][sq];
  2601.       u = ppos[sq];
  2602.       do
  2603.     {
  2604.       if (atk2[u] & ctlRQ)
  2605.         ScoreThreat;
  2606.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2607.       } while (u != sq);
  2608.     }
  2609.   if (HasKnight[c2])
  2610.     {
  2611.       pdir = nextdir[knight][sq];
  2612.       u = pdir[sq];
  2613.       do
  2614.     {
  2615.       if (atk2[u] & ctlNN)
  2616.         ScoreThreat;
  2617.       u = pdir[u];
  2618.       } while (u != sq);
  2619.     }
  2620.   *s += (KSFTY * KTHRT[cnt]) / 16;
  2621.  
  2622.   cnt = 0;
  2623.   ok = false;
  2624.   pdir = nextpos[king][sq];
  2625.   u = pdir[sq];
  2626.   do
  2627.     {
  2628.       if (board[u] == pawn)
  2629.     ok = true;
  2630.       if (atk2[u] > atk1[u])
  2631.     {
  2632.       ++cnt;
  2633.       if (atk2[u] & ctlQ)
  2634.         if (atk2[u] > ctlQ + 1 && atk1[u] < ctlQ)
  2635.           *s -= 4 * KSFTY;
  2636.     }
  2637.       u = pdir[u];
  2638.   } while (u != sq);
  2639.   if (!ok)
  2640.     *s -= KSFTY;
  2641.   if (cnt > 1)
  2642.     *s -= KSFTY;
  2643. }
  2644.  
  2645.  
  2646. static inline int
  2647. trapped (short int sq)
  2648.  
  2649. /*
  2650.   See if the attacked piece has unattacked squares to move to.
  2651.   The following must be true:
  2652.   c1 == color[sq]
  2653.   c2 == otherside[c1]
  2654. */
  2655.  
  2656. {
  2657.   register short u, piece;
  2658.   register unsigned char *ppos, *pdir;
  2659.  
  2660.   piece = board[sq];
  2661.   ppos = nextpos[ptype[c1][piece]][sq];
  2662.   pdir = nextdir[ptype[c1][piece]][sq];
  2663.   if (piece == pawn)
  2664.     {
  2665.       u = ppos[sq];    /* follow no captures thread */
  2666.       if (color[u] == neutral)
  2667.     {
  2668.       if (atk1[u] >= atk2[u])
  2669.         return (false);
  2670.       if (atk2[u] < ctlP)
  2671.         {
  2672.           u = ppos[u];
  2673.           if (color[u] == neutral && atk1[u] >= atk2[u])
  2674.         return (false);
  2675.         }
  2676.     }
  2677.       u = pdir[sq];    /* follow captures thread */
  2678.       if (color[u] == c2)
  2679.     return (false);
  2680.       u = pdir[u];
  2681.       if (color[u] == c2)
  2682.     return (false);
  2683.     }
  2684.   else
  2685.     {
  2686.       u = ppos[sq];
  2687.       do
  2688.     {
  2689.       if (color[u] != c1)
  2690.         if (atk2[u] == 0 || board[u] >= piece)
  2691.           return (false);
  2692.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2693.       } while (u != sq);
  2694.     }
  2695.   return (true);
  2696. }
  2697.  
  2698.  
  2699. static inline int
  2700. PawnValue (short int sq, short int side)
  2701.  
  2702. /*
  2703.   Calculate the positional value for a pawn on 'sq'.
  2704. */
  2705.  
  2706. {
  2707.   register short j, fyle, rank;
  2708.   register short s, a1, a2, in_square, r, e;
  2709.  
  2710.   a1 = (atk1[sq] & 0x4FFF);
  2711.   a2 = (atk2[sq] & 0x4FFF);
  2712.   rank = row (sq);
  2713.   fyle = column (sq);
  2714.   s = 0;
  2715.   if (c1 == white)
  2716.     {
  2717.       s = Mwpawn[sq];
  2718.       if ((sq == 11 && color[19] != neutral)
  2719.           || (sq == 12 && color[20] != neutral))
  2720.     s += PEDRNK2B;
  2721.       if ((fyle == 0 || PC1[fyle - 1] == 0)
  2722.       && (fyle == 7 || PC1[fyle + 1] == 0))
  2723.     s += ISOLANI[fyle];
  2724.       else if (PC1[fyle] > 1)
  2725.     s += PDOUBLED;
  2726.       if (a1 < ctlP && atk1[sq + 8] < ctlP)
  2727.     {
  2728.       s += BACKWARD[a2 & 0xFF];
  2729.       if (PC2[fyle] == 0)
  2730.         s += PWEAKH;
  2731.       if (color[sq + 8] != neutral)
  2732.         s += PBLOK;
  2733.     }
  2734.       if (PC2[fyle] == 0)
  2735.     {
  2736.       if (side == black)
  2737.         r = rank - 1;
  2738.       else
  2739.         r = rank;
  2740.       in_square = (row (bking) >= r && distance (sq, bking) < 8 - r);
  2741.       if (a2 == 0 || side == white)
  2742.         e = 0;
  2743.       else
  2744.         e = 1;
  2745.       for (j = sq + 8; j < 64; j += 8)
  2746.         if (atk2[j] >= ctlP)
  2747.           {
  2748.         e = 2;
  2749.         break;
  2750.           }
  2751.         else if (atk2[j] > 0 || color[j] != neutral)
  2752.           e = 1;
  2753.       if (e == 2)
  2754.         s += (stage * PassedPawn3[rank]) / 10;
  2755.       else if (in_square || e == 1)
  2756.         s += (stage * PassedPawn2[rank]) / 10;
  2757.       else if (emtl[black] > 0)
  2758.         s += (stage * PassedPawn1[rank]) / 10;
  2759.       else
  2760.         s += PassedPawn0[rank];
  2761.     }
  2762.     }
  2763.   else if (c1 == black)
  2764.     {
  2765.       s = Mbpawn[sq];
  2766.       if ((sq == 51 && color[43] != neutral)
  2767.       || (sq == 52 && color[44] != neutral))
  2768.     s += PEDRNK2B;
  2769.       if ((fyle == 0 || PC1[fyle - 1] == 0) &&
  2770.       (fyle == 7 || PC1[fyle + 1] == 0))
  2771.     s += ISOLANI[fyle];
  2772.       else if (PC1[fyle] > 1)
  2773.     s += PDOUBLED;
  2774.       if (a1 < ctlP && atk1[sq - 8] < ctlP)
  2775.     {
  2776.       s += BACKWARD[a2 & 0xFF];
  2777.       if (PC2[fyle] == 0)
  2778.         s += PWEAKH;
  2779.       if (color[sq - 8] != neutral)
  2780.         s += PBLOK;
  2781.     }
  2782.       if (PC2[fyle] == 0)
  2783.     {
  2784.       if (side == white)
  2785.         r = rank + 1;
  2786.       else
  2787.         r = rank;
  2788.       in_square = (row (wking) <= r && distance (sq, wking) < r + 1);
  2789.       if (a2 == 0 || side == black)
  2790.         e = 0;
  2791.       else
  2792.         e = 1;
  2793.       for (j = sq - 8; j >= 0; j -= 8)
  2794.         if (atk2[j] >= ctlP)
  2795.           {
  2796.         e = 2;
  2797.         break;
  2798.           }
  2799.         else if (atk2[j] > 0 || color[j] != neutral)
  2800.           e = 1;
  2801.       if (e == 2)
  2802.         s += (stage * PassedPawn3[7 - rank]) / 10;
  2803.       else if (in_square || e == 1)
  2804.         s += (stage * PassedPawn2[7 - rank]) / 10;
  2805.       else if (emtl[white] > 0)
  2806.         s += (stage * PassedPawn1[7 - rank]) / 10;
  2807.       else
  2808.         s += PassedPawn0[7 - rank];
  2809.     }
  2810.     }
  2811.   if (a2 > 0)
  2812.     {
  2813.       if (a1 == 0 || a2 > ctlP + 1)
  2814.     {
  2815.       s += HUNGP;
  2816.       ++hung[c1];
  2817.       if (trapped (sq))
  2818.         ++hung[c1];
  2819.     }
  2820.       else
  2821.     if (a2 > a1)
  2822.       s += ATAKD;
  2823.     }
  2824.   return (s);
  2825. }
  2826.  
  2827.  
  2828. static inline int
  2829. KnightValue (short int sq, short int side)
  2830.  
  2831. /*
  2832.   Calculate the positional value for a knight on 'sq'.
  2833. */
  2834.  
  2835. {
  2836.   register short s, a2, a1;
  2837.  
  2838.   s = Mknight[c1][sq];
  2839.   a2 = (atk2[sq] & 0x4FFF);
  2840.   if (a2 > 0)
  2841.     {
  2842.       a1 = (atk1[sq] & 0x4FFF);
  2843.       if (a1 == 0 || a2 > ctlBN + 1)
  2844.     {
  2845.       s += HUNGP;
  2846.       ++hung[c1];
  2847.       if (trapped (sq))
  2848.         ++hung[c1];
  2849.     }
  2850.       else
  2851.     if (a2 >= ctlBN || a1 < ctlP)
  2852.       s += ATAKD;
  2853.     }
  2854.   return (s);
  2855. }
  2856.  
  2857.  
  2858. static inline int
  2859. BishopValue (short int sq, short int side)
  2860.  
  2861. /*
  2862.   Calculate the positional value for a bishop on 'sq'.
  2863. */
  2864.  
  2865. {
  2866.   register short a2, a1;
  2867.   short s, mob;
  2868.  
  2869.   s = Mbishop[c1][sq];
  2870.   BRscan (sq, &s, &mob);
  2871.   s += BMBLTY[mob];
  2872.   a2 = (atk2[sq] & 0x4FFF);
  2873.   if (a2 > 0)
  2874.     {
  2875.       a1 = (atk1[sq] & 0x4FFF);
  2876.       if (a1 == 0 || a2 > ctlBN + 1)
  2877.     {
  2878.       s += HUNGP;
  2879.       ++hung[c1];
  2880.       if (trapped (sq))
  2881.         ++hung[c1];
  2882.     }
  2883.       else
  2884.     if (a2 >= ctlBN || a1 < ctlP)
  2885.       s += ATAKD;
  2886.     }
  2887.   return (s);
  2888. }
  2889.  
  2890.  
  2891. static inline int
  2892. RookValue (short int sq, short int side)
  2893.  
  2894. /*
  2895.   Calculate the positional value for a rook on 'sq'.
  2896. */
  2897.  
  2898. {
  2899.   register short fyle, a2, a1;
  2900.   short s, mob;
  2901.  
  2902.   s = RookBonus;
  2903.   BRscan (sq, &s, &mob);
  2904.   s += RMBLTY[mob];
  2905.   fyle = column (sq);
  2906.   if (PC1[fyle] == 0)
  2907.     s += RHOPN;
  2908.   if (PC2[fyle] == 0)
  2909.     s += RHOPNX;
  2910.   if (pmtl[c2] > 100 && row (sq) == rank7[c1])
  2911.     s += 10;
  2912.   if (stage > 2)
  2913.     s += 14 - taxicab (sq, EnemyKing);
  2914.   a2 = (atk2[sq] & 0x4FFF);
  2915.   if (a2 > 0)
  2916.     {
  2917.       a1 = (atk1[sq] & 0x4FFF);
  2918.       if (a1 == 0 || a2 > ctlR + 1)
  2919.     {
  2920.       s += HUNGP;
  2921.       ++hung[c1];
  2922.  
  2923.       if (trapped (sq))
  2924.         ++hung[c1];
  2925.     }
  2926.       else
  2927.     if (a2 >= ctlR || a1 < ctlP)
  2928.       s += ATAKD;
  2929.     }
  2930.   return (s);
  2931. }
  2932.  
  2933.  
  2934. static inline int
  2935. QueenValue (short int sq, short int side)
  2936.  
  2937. /*
  2938.   Calculate the positional value for a queen on 'sq'.
  2939. */
  2940.  
  2941. {
  2942.   register short s, a2, a1;
  2943.  
  2944.   s = (distance (sq, EnemyKing) < 3) ? 12 : 0;
  2945.   if (stage > 2)
  2946.     s += 14 - taxicab (sq, EnemyKing);
  2947.   a2 = (atk2[sq] & 0x4FFF);
  2948.   if (a2 > 0)
  2949.     {
  2950.       a1 = (atk1[sq] & 0x4FFF);
  2951.       if (a1 == 0 || a2 > ctlQ + 1)
  2952.     {
  2953.       s += HUNGP;
  2954.       ++hung[c1];
  2955.       if (trapped (sq))
  2956.         ++hung[c1];
  2957.     }
  2958.       else
  2959.     if (a2 >= ctlQ || a1 < ctlP)
  2960.       s += ATAKD;
  2961.     }
  2962.   return (s);
  2963. }
  2964.  
  2965.  
  2966. static inline int
  2967. KingValue (short int sq, short int side)
  2968.  
  2969. /*
  2970.   Calculate the positional value for a king on 'sq'.
  2971. */
  2972.  
  2973. {
  2974.   register short fyle, a2, a1;
  2975.   short s;
  2976.  
  2977.   s = Mking[c1][sq];
  2978.   if (KSFTY > 0)
  2979.     if (Developed[c2] || stage > 0)
  2980.       KingScan (sq, &s);
  2981.   if (castld[c1])
  2982.     s += KCASTLD;
  2983.   else if (Mvboard[kingP[c1]])
  2984.     s += KMOVD;
  2985.  
  2986.   fyle = column (sq);
  2987.   if (PC1[fyle] == 0)
  2988.     s += KHOPN;
  2989.   if (PC2[fyle] == 0)
  2990.     s += KHOPNX;
  2991.   switch (fyle)
  2992.     {
  2993.     case 5:
  2994.       if (PC1[7] == 0)
  2995.         s += KHOPN;
  2996.       if (PC2[7] == 0)
  2997.         s += KHOPNX;
  2998.       /* Fall through */
  2999.     case 4:
  3000.     case 6:
  3001.     case 0:
  3002.       if (PC1[fyle + 1] == 0)
  3003.         s += KHOPN;
  3004.       if (PC2[fyle + 1] == 0)
  3005.         s += KHOPNX;
  3006.       break;
  3007.     case 2:    
  3008.       if (PC1[0] == 0)
  3009.         s += KHOPN;
  3010.       if (PC2[0] == 0)
  3011.         s += KHOPNX;
  3012.       /* Fall through */
  3013.     case 3:
  3014.     case 1:
  3015.     case 7:  
  3016.       if (PC1[fyle - 1] == 0)
  3017.         s += KHOPN;
  3018.       if (PC2[fyle - 1] == 0)
  3019.         s += KHOPNX;
  3020.       break;
  3021.     default:
  3022.       /* Impossible! */
  3023.       break;
  3024.     }
  3025.  
  3026.   a2 = (atk2[sq] & 0x4FFF);
  3027.   if (a2 > 0)
  3028.     {
  3029.       a1 = (atk1[sq] & 0x4FFF);
  3030.       if (a1 == 0 || a2 > ctlK + 1)
  3031.     {
  3032.       s += HUNGP;
  3033.       ++hung[c1];
  3034.     }
  3035.       else
  3036.     s += ATAKD;
  3037.     }
  3038.   return (s);
  3039. }
  3040.  
  3041.  
  3042. void
  3043. ScorePosition (short int side, short int *score)
  3044.  
  3045. /*
  3046.   Perform normal static evaluation of board position. A score is generated
  3047.   for each piece and these are summed to get a score for each side.
  3048. */
  3049.  
  3050. {
  3051.   register short sq, s, i, xside;
  3052.   short pscore[2];
  3053.  
  3054.   UpdateWeights ();
  3055.   xside = otherside[side];
  3056.   pscore[white] = pscore[black] = 0;
  3057.  
  3058.   for (c1 = white; c1 <= black; c1++)
  3059.     {
  3060.       c2 = otherside[c1];
  3061.       atk1 = atak[c1];
  3062.       atk2 = atak[c2];
  3063.       PC1 = PawnCnt[c1];
  3064.       PC2 = PawnCnt[c2];
  3065.       for (i = PieceCnt[c1]; i >= 0; i--)
  3066.     {
  3067.       sq = PieceList[c1][i];
  3068.       switch (board[sq])
  3069.         {
  3070.         case pawn:
  3071.           s = PawnValue(sq, side);
  3072.           break;
  3073.         case knight:
  3074.           s = KnightValue(sq, side);
  3075.           break;
  3076.         case bishop:
  3077.           s = BishopValue(sq, side);
  3078.           break;
  3079.         case rook:
  3080.           s = RookValue(sq, side);
  3081.           break;
  3082.         case queen:
  3083.           s = QueenValue(sq, side);
  3084.           break;
  3085.         case king:
  3086.           s = KingValue(sq, side);
  3087.           break;
  3088.         default:
  3089.           s = 0;
  3090.           break;
  3091.         }
  3092.       pscore[c1] += s;
  3093.       svalue[sq] = s;
  3094.     }
  3095.     }
  3096.   if (hung[side] > 1)
  3097.     pscore[side] += HUNGX;
  3098.   if (hung[xside] > 1)
  3099.     pscore[xside] += HUNGX;
  3100.  
  3101.   *score = mtl[side] - mtl[xside] + pscore[side] - pscore[xside] + 10;
  3102.   if (dither)
  3103.     *score += urand () % dither;
  3104.  
  3105.   if (*score > 0 && pmtl[side] == 0)
  3106.     if (emtl[side] < valueR)
  3107.       *score = 0;
  3108.     else if (*score < valueR)
  3109.       *score /= 2;
  3110.   if (*score < 0 && pmtl[xside] == 0)
  3111.     if (emtl[xside] < valueR)
  3112.       *score = 0;
  3113.     else if (-*score < valueR)
  3114.       *score /= 2;
  3115.  
  3116.   if (mtl[xside] == valueK && emtl[side] > valueB)
  3117.     *score += 200;
  3118.   if (mtl[side] == valueK && emtl[xside] > valueB)
  3119.     *score -= 200;
  3120. }
  3121.  
  3122.  
  3123. static inline void
  3124. BlendBoard (const short int a[64], const short int b[64], short int c[64])
  3125. {
  3126.   register int sq;
  3127.  
  3128.   for (sq = 0; sq < 64; sq++)
  3129.     c[sq] = (a[sq] * (10 - stage) + b[sq] * stage) / 10;
  3130. }
  3131.  
  3132.  
  3133. static inline void
  3134. CopyBoard (const short int a[64], short int b[64])
  3135. {
  3136.   register int sq;
  3137.  
  3138.   for (sq = 0; sq < 64; sq++)
  3139.     b[sq] = a[sq];
  3140. }
  3141.  
  3142. void
  3143. ExaminePosition (void)
  3144.  
  3145. /*
  3146.   This is done one time before the search is started. Set up arrays
  3147.   Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the
  3148.   SqValue() function to determine the positional value of each piece.
  3149. */
  3150.  
  3151. {
  3152.   register short i, sq;
  3153.   short wpadv, bpadv, wstrong, bstrong, z, side, pp, j, k, val, Pd, fyle, rank;
  3154.   static short PawnStorm = false;
  3155.  
  3156.   ataks (white, atak[white]);
  3157.   ataks (black, atak[black]);
  3158.   UpdateWeights ();
  3159.   HasKnight[white] = HasKnight[black] = 0;
  3160.   HasBishop[white] = HasBishop[black] = 0;
  3161.   HasRook[white] = HasRook[black] = 0;
  3162.   HasQueen[white] = HasQueen[black] = 0;
  3163.   for (side = white; side <= black; side++)
  3164.     for (i = PieceCnt[side]; i >= 0; i--)
  3165.       switch (board[PieceList[side][i]])
  3166.     {
  3167.     case knight:
  3168.       ++HasKnight[side];
  3169.       break;
  3170.     case bishop:
  3171.       ++HasBishop[side];
  3172.       break;
  3173.     case rook:
  3174.       ++HasRook[side];
  3175.       break;
  3176.     case queen:
  3177.       ++HasQueen[side];
  3178.       break;
  3179.     }
  3180.   if (!Developed[white])
  3181.     Developed[white] = (board[1] != knight && board[2] != bishop &&
  3182.             board[5] != bishop && board[6] != knight);
  3183.   if (!Developed[black])
  3184.     Developed[black] = (board[57] != knight && board[58] != bishop &&
  3185.             board[61] != bishop && board[62] != knight);
  3186.   if (!PawnStorm && stage < 5)
  3187.     PawnStorm = ((column (wking) < 3 && column (bking) > 4) ||
  3188.          (column (wking) > 4 && column (bking) < 3));
  3189.  
  3190.   CopyBoard (pknight, Mknight[white]);
  3191.   CopyBoard (pknight, Mknight[black]);
  3192.   CopyBoard (pbishop, Mbishop[white]);
  3193.   CopyBoard (pbishop, Mbishop[black]);
  3194.   BlendBoard (KingOpening, KingEnding, Mking[white]);
  3195.   BlendBoard (KingOpening, KingEnding, Mking[black]);
  3196.  
  3197.   for (sq = 0; sq < 64; sq++)
  3198.     {
  3199.       fyle = column (sq);
  3200.       rank = row (sq);
  3201.       wstrong = bstrong = true;
  3202.       for (i = sq; i < 64; i += 8)
  3203.     if (Patak (black, i))
  3204.       {
  3205.         wstrong = false;
  3206.         break;
  3207.       }
  3208.       for (i = sq; i >= 0; i -= 8)
  3209.     if (Patak (white, i))
  3210.       {
  3211.         bstrong = false;
  3212.         break;
  3213.       }
  3214.       wpadv = bpadv = PADVNCM;
  3215.       if ((fyle == 0 || PawnCnt[white][fyle - 1] == 0) &&
  3216.       (fyle == 7 || PawnCnt[white][fyle + 1] == 0))
  3217.     wpadv = PADVNCI;
  3218.       if ((fyle == 0 || PawnCnt[black][fyle - 1] == 0) &&
  3219.       (fyle == 7 || PawnCnt[black][fyle + 1] == 0))
  3220.     bpadv = PADVNCI;
  3221.       Mwpawn[sq] = (wpadv * PawnAdvance[sq]) / 10;
  3222.       Mbpawn[sq] = (bpadv * PawnAdvance[63 - sq]) / 10;
  3223.       Mwpawn[sq] += PawnBonus;
  3224.       Mbpawn[sq] += PawnBonus;
  3225.       if (Mvboard[kingP[white]])
  3226.     {
  3227.       if ((fyle < 3 || fyle > 4) && distance (sq, wking) < 3)
  3228.         Mwpawn[sq] += PAWNSHIELD;
  3229.     }
  3230.       else if (rank < 3 && (fyle < 2 || fyle > 5))
  3231.     Mwpawn[sq] += PAWNSHIELD / 2;
  3232.       if (Mvboard[kingP[black]])
  3233.     {
  3234.       if ((fyle < 3 || fyle > 4) && distance (sq, bking) < 3)
  3235.         Mbpawn[sq] += PAWNSHIELD;
  3236.     }
  3237.       else if (rank > 4 && (fyle < 2 || fyle > 5))
  3238.     Mbpawn[sq] += PAWNSHIELD / 2;
  3239.       if (PawnStorm)
  3240.     {
  3241.       if ((column (wking) < 4 && fyle > 4) ||
  3242.           (column (wking) > 3 && fyle < 3))
  3243.         Mwpawn[sq] += 3 * rank - 21;
  3244.       if ((column (bking) < 4 && fyle > 4) ||
  3245.           (column (bking) > 3 && fyle < 3))
  3246.         Mbpawn[sq] -= 3 * rank;
  3247.     }
  3248.       Mknight[white][sq] += 5 - distance (sq, bking);
  3249.       Mknight[white][sq] += 5 - distance (sq, wking);
  3250.       Mknight[black][sq] += 5 - distance (sq, wking);
  3251.       Mknight[black][sq] += 5 - distance (sq, bking);
  3252.       Mbishop[white][sq] += BishopBonus;
  3253.       Mbishop[black][sq] += BishopBonus;
  3254.       for (i = PieceCnt[black]; i >= 0; i--)
  3255.     if (distance (sq, PieceList[black][i]) < 3)
  3256.       Mknight[white][sq] += KNIGHTPOST;
  3257.       for (i = PieceCnt[white]; i >= 0; i--)
  3258.     if (distance (sq, PieceList[white][i]) < 3)
  3259.       Mknight[black][sq] += KNIGHTPOST;
  3260.       if (wstrong)
  3261.     Mknight[white][sq] += KNIGHTSTRONG;
  3262.       if (bstrong)
  3263.     Mknight[black][sq] += KNIGHTSTRONG;
  3264.       if (wstrong)
  3265.     Mbishop[white][sq] += BISHOPSTRONG;
  3266.       if (bstrong)
  3267.     Mbishop[black][sq] += BISHOPSTRONG;
  3268.  
  3269.       if (HasBishop[white] == 2)
  3270.     Mbishop[white][sq] += 8;
  3271.       if (HasBishop[black] == 2)
  3272.     Mbishop[black][sq] += 8;
  3273.       if (HasKnight[white] == 2)
  3274.     Mknight[white][sq] += 5;
  3275.       if (HasKnight[black] == 2)
  3276.     Mknight[black][sq] += 5;
  3277.  
  3278.       Kfield[white][sq] = Kfield[black][sq] = 0;
  3279.       if (distance (sq, wking) == 1)
  3280.     Kfield[black][sq] = KATAK;
  3281.       if (distance (sq, bking) == 1)
  3282.     Kfield[white][sq] = KATAK;
  3283.  
  3284.       Pd = 0;
  3285.       for (k = 0; k <= PieceCnt[white]; k++)
  3286.     {
  3287.       i = PieceList[white][k];
  3288.       if (board[i] == pawn)
  3289.         {
  3290.           pp = true;
  3291.           if (row (i) == 6)
  3292.         z = i + 8;
  3293.           else
  3294.         z = i + 16;
  3295.           for (j = i + 8; j < 64; j += 8)
  3296.         if (Patak (black, j) || board[j] == pawn)
  3297.           {
  3298.             pp = false;
  3299.             break;
  3300.           }
  3301.           if (pp)
  3302.         Pd += 5 * taxicab (sq, z);
  3303.           else
  3304.         Pd += taxicab (sq, z);
  3305.         }
  3306.     }
  3307.       for (k = 0; k <= PieceCnt[black]; k++)
  3308.     {
  3309.       i = PieceList[black][k];
  3310.       if (board[i] == pawn)
  3311.         {
  3312.           pp = true;
  3313.           if (row (i) == 1)
  3314.         z = i - 8;
  3315.           else
  3316.         z = i - 16;
  3317.           for (j = i - 8; j >= 0; j -= 8)
  3318.         if (Patak (white, j) || board[j] == pawn)
  3319.           {
  3320.             pp = false;
  3321.             break;
  3322.           }
  3323.           if (pp)
  3324.         Pd += 5 * taxicab (sq, z);
  3325.           else
  3326.         Pd += taxicab (sq, z);
  3327.         }
  3328.     }
  3329.       if (Pd != 0)
  3330.     {
  3331.       val = (Pd * stage2) / 10;
  3332.       Mking[white][sq] -= val;
  3333.       Mking[black][sq] -= val;
  3334.     }
  3335.     }
  3336. }
  3337.  
  3338. void
  3339. UpdateWeights (void)
  3340.  
  3341. /*
  3342.   If material balance has changed, determine the values for the positional
  3343.   evaluation terms.
  3344. */
  3345.  
  3346. {
  3347.   register short tmtl, s1;
  3348.  
  3349.   emtl[white] = mtl[white] - pmtl[white] - valueK;
  3350.   emtl[black] = mtl[black] - pmtl[black] - valueK;
  3351.   tmtl = emtl[white] + emtl[black];
  3352.   s1 = (tmtl > 6600) ? 0 : ((tmtl < 1400) ? 10 : (6600 - tmtl) / 520);
  3353.   if (s1 != stage)
  3354.     {
  3355.       stage = s1;
  3356.       stage2 = (tmtl > 3600) ? 0 : ((tmtl < 1400) ? 10 : (3600 - tmtl) / 220);
  3357.       PEDRNK2B = -15;    /* centre pawn on 2nd rank & blocked */
  3358.       PBLOK = -4;        /* blocked backward pawn */
  3359.       PDOUBLED = -14;    /* doubled pawn */
  3360.       PWEAKH = -4;        /* weak pawn on half open file */
  3361.       PAWNSHIELD = 10 - stage;    /* pawn near friendly king */
  3362.       PADVNCM = 10;        /* advanced pawn multiplier */
  3363.       PADVNCI = 7;        /* muliplier for isolated pawn */
  3364.       PawnBonus = stage;
  3365.       
  3366.       KNIGHTPOST = (stage + 2) / 3;    /* knight near enemy pieces */
  3367.       KNIGHTSTRONG = (stage + 6) / 2;    /* occupies pawn hole */
  3368.       
  3369.       BISHOPSTRONG = (stage + 6) / 2;    /* occupies pawn hole */
  3370.       BishopBonus = 2 * stage;
  3371.       
  3372.       RHOPN = 10;        /* rook on half open file */
  3373.       RHOPNX = 4;
  3374.       RookBonus = 6 * stage;
  3375.       
  3376.       XRAY = 8;        /* Xray attack on piece */
  3377.       PINVAL = 10;        /* Pin */
  3378.       
  3379.       KHOPN = (3 * stage - 30) / 2;    /* king on half open file */
  3380.       KHOPNX = KHOPN / 2;
  3381.       KCASTLD = 10 - stage;
  3382.       KMOVD = -40 / (stage + 1);    /* king moved before castling */
  3383.       KATAK = (10 - stage) / 2;    /* B,R attacks near enemy king */
  3384.       if (stage < 8)
  3385.     KSFTY = 16 - 2 * stage;
  3386.       else
  3387.     KSFTY = 0;
  3388.       
  3389.       ATAKD = -6;        /* defender > attacker */
  3390.       HUNGP = -8;        /* each hung piece */
  3391.       HUNGX = -12;        /* extra for >1 hung piece */
  3392.     }
  3393. }
  3394.